Results 1 to 16 of 16
 11262007, 06:35 PM #1Member
 Join Date
 Nov 2007
 Posts
 5
 Rep Power
 0
 11272007, 04:58 AM #2Senior 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)); }
 08202008, 07:25 PM #3Member
 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?
 08202008, 07:39 PM #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; 08202008 at 07:41 PM.
 08202008, 11:50 PM #5
You are not, by chance, violating the number one rule, are you:
Never use float or double for money. Its a sin.
 Never use float or double for money. Its a sin.
 08212008, 01:02 AM #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.

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.
 01232009, 01:45 AM #9Member
 Join Date
 Jan 2009
 Posts
 1
 Rep Power
 0
 01232009, 07:39 AM #10
 01232009, 06:06 PM #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.
 11122009, 07:38 PM #12Member
 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.
 11242009, 04:21 AM #13Member
 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.
 11242009, 11:44 AM #14Member
 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?
 03102010, 12:10 AM #15Member
 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
 03102010, 01:04 AM #16
Similar Threads

round to two decimal places
By javaMike in forum New To JavaReplies: 3Last Post: 12242011, 03:01 AM 
Java calculator decimal
By cart1443 in forum New To JavaReplies: 2Last Post: 04162008, 02:19 PM 
How do I convert a decimal value to hexadecimal with double precision (64 bit)
By SKaur in forum New To JavaReplies: 7Last Post: 01122008, 10:02 PM 
Capping decimal places
By Rageagainst20 in forum New To JavaReplies: 1Last Post: 12202007, 10:28 PM 
Help with java Rounding
By silvia in forum New To JavaReplies: 1Last Post: 07202007, 08:25 AM
Bookmarks