Formatting numbers using DecimalFormat
Asked Answered
C

6

19

I am trying to format prices using DecimalFormat, but this isn't working for all variations.

DecimalFormat df = new DecimalFormat("0.##")
df.format(7.8)
df.format(85.0)

prints

7.80

and

85

but "7.79999" gets formatted as "7.8", not "7.80". I have tried doing things this way

DecimalFormat df = new DecimalFormat("0.00")

to force two dp, but then "85.0" gets formatted as "85.00" not "85"!

Is there a way of capturing all variations, so that prices are printed either as #, ##, or #.##? For example:

5, 55, 5.55, 5.50, 500, 500.40

Chloride answered 16/5, 2013 at 9:23 Comment(1)
#5033904Adsorb
S
15

This doesn't seem to be solved by a single formatter. I suggest you use "0.00" format and replace ".00" with an empty string.

public static String myFormat(double number) {
  DecimalFormat df = new DecimalFormat("0.00");
  return df.format(number).replaceAll("\\.00$", "");
}
Sitka answered 16/5, 2013 at 9:37 Comment(1)
What if decimal separator is comma (it's taken from default locale, no)? Will it still work?Libya
S
27

There is a slight difference between these two formats. The "#.##" means it will print the number with maximum two decimal places whereas "#.00" means it will always display two decimal places and if the decimal places are less than two, it will replace them with zeros. see the example below with output.

public static final DecimalFormat df1 = new DecimalFormat( "#.##" );
public static final DecimalFormat df2 = new DecimalFormat( "#.00" );

System.out.println(df1.format(7.80));
System.out.println(df1.format(85));
System.out.println(df1.format(85.786));

System.out.println(df2.format(7.80));
System.out.println(df2.format(85));
System.out.println(df2.format(85.786));

And the output will be

7.8
85
85.79

7.80
85.00
85.79
Stegman answered 16/5, 2013 at 9:37 Comment(3)
Yes I know thanks, but I need something that covers both these functions, so that 7.79999 prints 7.80 and 85.00 prints 85Chloride
I am afraid it is not possible with the built in formatters. You will have to format the resulting String manually. Perhaps by removing the last characters if they are 00sStegman
Ooops, did not see the answer from @EvgeniyDorofeev, that's the only possibility for youStegman
S
15

This doesn't seem to be solved by a single formatter. I suggest you use "0.00" format and replace ".00" with an empty string.

public static String myFormat(double number) {
  DecimalFormat df = new DecimalFormat("0.00");
  return df.format(number).replaceAll("\\.00$", "");
}
Sitka answered 16/5, 2013 at 9:37 Comment(1)
What if decimal separator is comma (it's taken from default locale, no)? Will it still work?Libya
C
5

I don't think it's possible, at least not with Java SE formatters. You need to make a custom formatter. I would do it like this

String res = df.format(number).replace(".00", "");
Corrida answered 16/5, 2013 at 9:32 Comment(0)
N
3

Use the BigDecimal number class instead:

e.g. if n is a BigDecimal, then you can use

String s = NumberFormat.getCurrencyInstance().format(n);

By the way, it's best practice to use BigDecimal when working with money.

Nympholepsy answered 16/5, 2013 at 9:29 Comment(2)
Thanks, but this doesn't remove the trailing zero's, e.g. 5.0 and 5.00Chloride
Ok, compare the string to this regex [0-9]*[.]0+: if (s.matches("[0-9]*[.]0+")) then "strip off the stuff on and after the period". This will also take care of things like 5. and even 3 digit currencies (there are some of these around still).Nympholepsy
D
0

You can try with:

DecimalFormat df = new DecimalFormat("#.##",new DecimalFormatSymbols(Locale.US));
Dewhurst answered 16/12, 2020 at 17:12 Comment(1)
Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.Pastelki
A
-2
System.out.println(new java.text.DecimalFormat("#.##").format(5.00));

This will print 5

System.out.println(new java.text.DecimalFormat("#.00").format(500.401));

This will print 500.40

Aachen answered 30/1, 2018 at 2:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.