2

Good day.

I need to format a number in java. So far I have this:

DecimalFormat df2 = new DecimalFormat( "#,###,###,##0.00" );
System.out.println(new Double(df2.format(balance)).doubleValue());

But it prints out this

110.0
121.0
133.1
146.41
161.05

But I need it to be with two digits in fraction part. How do I do it?

1
  • why do you reconvert it to a double if you just want to parse and print it? Commented Nov 12, 2012 at 9:45

6 Answers 6

2

You don't have to get double value from formatted string. Just use formatted string, which is returned from format() method of DecimalFormat. So your code should be like the following:

DecimalFormat df2 = new DecimalFormat( "#,###,###,##0.00" );
...
System.out.println(df2.format(balance));

Your original code:

System.out.println(new Double(df2.format(balance)).doubleValue());

What you did in your code is: format the double value to string(which is formatted as you specified in the DecimalFormat instance). Then you convert the formatted string to Double instance and get double value from the object, which is double. And then printed it to console. So the formatted string is gone, and the double value is printed as normal.

Sign up to request clarification or add additional context in comments.

Comments

1

"But I need it to be with two digits in fraction part. How do I do it?"

DecimalFormat df2 = new DecimalFormat( );
df2.setMinimumFractionDigits(2);
df2.setMaximumFractionDigits(2);

System.out.println(df2.format(balance));

Comments

0

You could also use the setMinimumFractionDigits method of DecimalFormat

df2.setMinimumFractionDigits(2);

Comments

0

your decimal format is right, but what you are doing before you print this out is new Double(df2.format(balance)) which create new instant of double, which ignores your formatting.

so if you want to display or log your value df2.format(balance) this should be enough

ie:

System.out.println(df2.format(balance));

Comments

0

Try this pattern for formatting #,###,###,##.##-

DecimalFormat df2 = new DecimalFormat( "#,###,###,##.##" );
System.out.println(df2.format(balance));

Comments

0

This should be sufficient:

DecimalFormat df2 = new DecimalFormat("#,##0.00");
System.out.println(df2.format(balance));

The grouping for separator will follow "the interval between the last one and the end of the integer". So there is no benefit from over-specify. Example from the documentation of DecimalFormat:

The grouping separator is commonly used for thousands, but in some countries it separates ten-thousands. The grouping size is a constant number of digits between the grouping characters, such as 3 for 100,000,000 or 4 for 1,0000,0000. If you supply a pattern with multiple grouping characters, the interval between the last one and the end of the integer is the one that is used. So "#,##,###,####" == "######,####" == "##,####,####".

Another thing is that .format() method already output a String, so there is no point in converting it to double. It will cause Exception to be thrown when balance is more than 1000 (the point when separator comes into effect, and Double class cannot parse the String with separator).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.