0

So im having a double and i want my JLabel to display it, i do it like this:

   jlabel.setText(String.valueOf(80.99999D)); // just an example number

But in addition to that, i would like to cut down the double number to maximum 2 digits behind the decimal point. So in the example above i would like the JLabel to only display 80.99. Is there any way to do it?

J.Dupla
  • 65
  • 4
  • 1
    Possible duplicate of [How to round a number to n decimal places in Java](https://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java) – Jacob G. Apr 09 '18 at 14:01

1 Answers1

3

You can solve this by using String.format

String.format("%.2f", 80.99999);

The number after the . defines, how many digits are displayed after the period (max). If you write .02f you will always have 2 digits after the period.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Robin M.
  • 96
  • 1
  • 5
  • String.format would round the number, so: `String.format("%.2f", 80.99999)` would give you `81.00`. If you really don't want to round up, see this [Stack Overflow answer](https://stackoverflow.com/questions/29462708/how-do-i-format-double-input-in-java-without-rounding-it?rq=1). – zazzy78 Apr 09 '18 at 14:02