# rounding double to two decimal places

• 11-26-2007, 05:35 PM
javaMike
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
• 11-27-2007, 03:58 AM
staykovmarin
DecimalFormat really is the easiest option to use:
Code:

```double roundTwoDecimals(double d) {                 DecimalFormat twoDForm = new DecimalFormat("#.##");                 return Double.valueOf(twoDForm.format(d)); }```
• 08-20-2008, 06:25 PM
shalomshachne
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?
• 08-20-2008, 06:39 PM
Norm
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.
• 08-20-2008, 10:50 PM
fishtoprecords
You are not, by chance, violating the number one rule, are you:

Never use float or double for money. Its a sin.
• 08-20-2008, 11:59 PM
Fubarable
Quote:

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?
• 08-21-2008, 12:02 AM
fishtoprecords
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.
• 08-21-2008, 01:32 AM
Fubarable
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.
• 01-23-2009, 12:45 AM
netstat1
So what do you suggest using? BigDecimal?

Quote:

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.

• 01-23-2009, 06:39 AM
fishtoprecords
Quote:

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.
• 01-23-2009, 05:06 PM
Steve11235
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.
• 11-12-2009, 06:38 PM
Joao

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.
• 11-24-2009, 03:21 AM
Spaznur
public static double round2(double num) {
double result = value * 100;
result = Math.round(result);
result = result / 100;
return result;
}

This works fine.
• 11-24-2009, 10:44 AM
Menre
Maybe nice to blame teachers and students on this issue. But I aggree with these questions.

Quote:

...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?
• 03-09-2010, 11:10 PM
vanstacks
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
• 03-10-2010, 12:04 AM
Steve11235
Use BigDecimal. That what it's for.