# Thread: rounding double to two decimal places

1. Member Join Date
Nov 2007
Posts
5
Rep Power
0

## rounding double to two decimal places

is there an easy way to round a double to two decimal places?

i.e. 1.98999 would round to 1.99

the math.round method only rounds to nearest integer.

decimalformat class converts number to a string.

i figure there must be an easy way to do this...?? pls advise 2. Senior Member Join Date
Nov 2007
Location
Newport, WA
Posts
141
Rep Power
0

## DecimalFormat really is the easiest option to use:
Java Code:
```double roundTwoDecimals(double d) {
DecimalFormat twoDForm = new DecimalFormat("#.##");
return Double.valueOf(twoDForm.format(d));
}``` 3. Member Join Date
Aug 2008
Posts
1
Rep Power
0

## Thanks for help with that...

Thanks for your help with that post. Am I insane, or does anyone else think that it's crazy not to have a simple already built function in Java to round a double to specified # of decimal places?

Am I missing something? Why would everyone want to custom code this function? 4. ## Another way to change the value of the double, you'd need to do something like this:
double dbl = 12.3456;
int ix = (int)(dbl * 100.0); // scale it
double dbl2 = ((double)ix)/100.0;
System.out.println("dbl=" + dbl + ", dbl2=" + dbl2); //dbl=12.3456, dbl2=12.34

To round, you'd need to add in some value.
Last edited by Norm; 08-20-2008 at 06:41 PM. 5. ## You are not, by chance, violating the number one rule, are you:

Never use float or double for money. Its a sin. 6. ## Never use float or double for money. Its a sin.
I can't disagree with you as you know a heck of a lot more Java than I'll ever know, but do you think this rule needs to be rigidly enforced for folks taking intro to Java courses? 7. ## I think teaching the use of float/double for currency is a mortal sin.

Sadly, some teachers don't know any better.

On this, and other forums, you see folks just starting out, clearly doing homework, and they all use floats or doubles.

The real problem is that it works for wimpy testing, and when you put it in production, stuff doesn't add up, the accountants get very mad, and you have a real mess to fix.

Cobol had fixed point built in from day one. For a reason. 8. ## Again, can't argue with any of that. As you state though, most teachers don't know any better, and most students here are coding with constraints set by these same teachers.

Again, thanks for posting your thoughts on this. 9. Member Join Date
Jan 2009
Posts
1
Rep Power
0

## So what do you suggest using? BigDecimal? Originally Posted by fishtoprecords You are not, by chance, violating the number one rule, are you:

Never use float or double for money. Its a sin. 10. ##  Originally Posted by netstat1 So what do you suggest using? BigDecimal?
You can if you must, but for most folks, you can simply store money as "pennies" rather that "dollars and cents" and use integer.

Since an integer can hold +-2billion, that is +- 2 billion cents, or 20 million dollars. For most applications that is enough. 11. ## Fish is right when you do have to deal with fractional amounts.

BigDecimal is specifically designed to handle very large values, exact values, calculations with a bunch of specific rounding methods, etc. 12. Member Join Date
Nov 2009
Posts
1
Rep Power
0

## What about:

public static double roundToDecimals(double d, int c) {
int temp=(int)((d*Math.pow(10,c)));
return (((double)temp)/Math.pow(10,c));
}

It gives you control of how many numbers after the point are needed. d=number to round; c = number of decimal places. 13. Member Join Date
Nov 2009
Posts
1
Rep Power
0

## public static double round2(double num) {
double result = value * 100;
result = Math.round(result);
result = result / 100;
return result;
}

This works fine. 14. Member Join Date
Feb 2008
Posts
79
Rep Power
0

## Maybe nice to blame teachers and students on this issue. But I aggree with these questions.

...not to have a simple already built function in Java to round a double to specified # of decimal places?
... Why would everyone want to custom code this function? 15. Member Join Date
Mar 2010
Posts
1
Rep Power
0

## If my code have many variables that need to have 2 decimal place format, then I have to use the Math.round function for every single one of them?

Or, there's any other way?

Thank you 16. ## Use BigDecimal. That what it's for. #### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•