322

I am dealing with lot of double values in my application, is there is any easy way to handle the formatting of decimal values in Java?

Is there any other better way of doing it than

 DecimalFormat df = new DecimalFormat("#.##");

What i want to do basically is format double values like

23.59004  to 23.59

35.7  to 35.70

3.0 to 3.00

9 to 9.00
Community
  • 1
  • 1
Rajesh Pantula
  • 10,061
  • 9
  • 43
  • 52
  • 1
    easier than one additional line? I'm pretty sure that's the simplest, clearest way to format a decimal. Do be aware that if this program will be used by people in different locales the DecimalFormat will use a ',' or '.' as appropriate for the decimal separator. This caused some problems in a project I was working on recently: writing to a textbox and reading the wrong value from it. – Dogmatixed Jan 11 '12 at 13:18
  • 1
    Thats what im very much worried of, I read that DecimalFormat by default locale. Can u please tell me how you get this working with localization? – Rajesh Pantula Jan 11 '12 at 13:21
  • I used JFormattedTextFields for input, populating them using setValue() and reading from them using getValue(). For places where it required a decimal, I used the DecimalFormatSymbols object. – Dogmatixed Jan 11 '12 at 21:02
  • 1
    There is also an easier way using `Math.round()` method. Topic solving this problem is here: [link](http://stackoverflow.com/questions/11701399/round-up-to-2-decimal-places-in-java) – frank17 Nov 19 '15 at 23:04

2 Answers2

534

No, there is no better way.

Actually you have an error in your pattern. What you want is:

DecimalFormat df = new DecimalFormat("#.00"); 

Note the "00", meaning exactly two decimal places.

If you use "#.##" (# means "optional" digit), it will drop trailing zeroes - ie new DecimalFormat("#.##").format(3.0d); prints just "3", not "3.00".

Nicklas A.
  • 6,501
  • 7
  • 40
  • 65
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Actually, formatting 3.0 with #.## will print 3. – Nicklas A. Apr 02 '13 at 04:29
  • what about 0.99 will it converted to .99 or 0.99? – Mubashar Jun 20 '14 at 01:47
  • 40
    @MubasharAhmad `".99"` - the `#` means "optional zeroes". If you want to preserve the leading zero (ie output `"0.99"`) use the pattern `"#0.00"` – Bohemian Jun 20 '14 at 02:42
  • 11
    you can use String num = String.format("%.2f",23.59004); and then print num. it will print 23.59. – Manoj Aug 19 '14 at 14:31
  • Per @ohcibi's link, this is not the only way. `String.format` can also do it, and it's a little easier to work with too. – BrainSlugs83 Oct 06 '14 at 20:05
  • 7
    Pay attention to the fact that the DecimalFormat will change separators according to the current machine's locale. So it's ok for printing value to screen but unreliable if you have to send values to be parsed somewhere else (e.g.: a database). The same applies to String.format(). – capitano666 Sep 30 '15 at 08:57
  • @Bohemian , I just ran a program with 0.99 and #.## keeps the leading zeros. The display 0.99 – Snake May 05 '16 at 22:19
  • See http://stackoverflow.com/questions/5054132/how-to-change-the-decimal-separator-of-decimalformat-from-comma-to-dot-point if you wish to ensure you have constant separator characters. – neilireson Jun 12 '16 at 20:20
  • Accepted answer does not work with values between 0.01 and 0.99 . Correct one should be the following: DecimalFormat df = new DecimalFormat("#0.00"); – faruk.kuscan Sep 29 '16 at 13:09
  • not a perfect one, such as when the value is `0.01` you only got `.01` – XYz Amos May 12 '17 at 08:55
  • According to JavaDoc, DecimalFormat is not thread safe. Although this answer works just fine if you synchronize access to the formatter. – akagixxer Jan 25 '18 at 20:19
  • @akagixxer most of the original classes in`java.text` are not thread safe. I wouldn't bother with synchronizing; just create a new one every time: `new DecimalFormat("#.00").format(myNumber)` – Bohemian Jan 25 '18 at 21:43
  • 1
    @Bohemian Yes, another valid option. I was just mentioning that because not everyone is aware of it. With most of these utilities there comes a point at which a ThreadLocal<> instance is more performant than the overhead of re-initializing a new instance every time. Like everything, it just depends on how the application is going to use it and which solution is best (i.e. unsychronized, synchronized, new every time, ThreadLocal etc). – akagixxer Jan 26 '18 at 19:08
380

An alternative is to use String.format:

double[] arr = { 23.59004,
    35.7,
    3.0,
    9
};

for ( double dub : arr ) {
  System.out.println( String.format( "%.2f", dub ) );
}

output:

23.59
35.70
3.00
9.00

You could also use System.out.format (same method signature), or create a java.util.Formatter which works in the same way.

OpenSauce
  • 8,533
  • 1
  • 24
  • 29