Results 1 to 16 of 16
- 11-26-2007, 05:35 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 5
- Rep Power
- 0
- 11-27-2007, 03:58 AM #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)); }
- 08-20-2008, 06:25 PM #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?
- 08-20-2008, 06: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; 08-20-2008 at 06:41 PM.
- 08-20-2008, 10: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.
-
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?Never use float or double for money. Its a sin.
- 08-21-2008, 12: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.
- 01-23-2009, 12:45 AM #9
Member
- Join Date
- Jan 2009
- Posts
- 1
- Rep Power
- 0
- 01-23-2009, 06:39 AM #10
- 01-23-2009, 05: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.
- 11-12-2009, 06:38 PM #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.
- 11-24-2009, 03:21 AM #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.
- 11-24-2009, 10:44 AM #14
Member
- Join Date
- Feb 2008
- Posts
- 78
- 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?
- 03-09-2010, 11:10 PM #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
- 03-10-2010, 12:04 AM #16
Similar Threads
-
round to two decimal places
By javaMike in forum New To JavaReplies: 3Last Post: 12-24-2011, 02:01 AM -
Java calculator decimal
By cart1443 in forum New To JavaReplies: 2Last Post: 04-16-2008, 01: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: 01-12-2008, 09:02 PM -
Capping decimal places
By Rageagainst20 in forum New To JavaReplies: 1Last Post: 12-20-2007, 09:28 PM -
Help with java Rounding
By silvia in forum New To JavaReplies: 1Last Post: 07-20-2007, 07:25 AM


LinkBack URL
About LinkBacks


Bookmarks