10-30-2009 10:30 AM
Hi, i am using this (below) to round a double to 2 places but i seem to remember that this will mean my program won't operate on version 4.5 and below. Does anybody have a way to round a double to 2 places without os version problems please?
public static final double roundDouble(double d, int places) {
return net.rim.device.api.util.MathUtilities.round(d * net.rim.device.api.util.MathUtilities.pow(10, (double) places)) / net.rim.device.api.util.MathUtilities.pow(10,
(double) places);
}
Solved! Go to Solution.
10-30-2009 10:50 AM - edited 10-30-2009 10:56 AM
double round(double d)
{
String str = Double.toString(d);
int i = str.indexOf('.');
if(i >= 0)
{
double floor;
if(i == 0)
{
floor = 0.0;
}
else
{
double floor = Double.valueOf(str.substring(0, i));
}
if(Integer.parseInt(str.substring(i+1, i+3) < 50)
{
return floor;
}
else
{
if(d < 0)
{
return floor - 1.0;
}
else
{
return floor + 1.0;
}
}
}
return d;
}
10-30-2009 12:32 PM - edited 10-30-2009 02:41 PM
Aside from the odd compilation error, I think the code given above will fail as soon as the double goes to Exponential format when converted to String.
I guess one option is to multiply the double by 100, add 0.5, stick the result in a long, then back to a double and divide by 100. As suggested by Dfallak below, you would need to make sure that the double value did not exceed the capabilities of a long (see Long.MAX_VALUE and Long.MIN_VALUE)
Alternatively something like the following.
double d = 1000000000.005d;
double test = (d % 0.01d);
double rounded;
if ( test < 0.005 ) {
rounded = d - test;
} else {
rounded = d + (0.01 - test);
}
10-30-2009 12:45 PM - edited 10-30-2009 12:46 PM
The reason I didn't give the 'convert to long' solution was that a long doesn't have as big of a domain as a double, so it will also fail in cases where the double is outside the domain of long values.
I'm not sure how well the '%' operator works on doubles.
10-30-2009 01:51 PM
"fail in cases where the double is outside the domain of long values" Agree, I should have made that comment in my original post. I'll add it in now.
"I'm not sure how well the '%' operator works on doubles" I did some testing before posting my solution, because I wasn't sure either. In fact, doubles really are a problem when people put things like cents in them, because they are potentially not an accurate representation. I mean you can get some really weird things like subtract .004 from 1,000,000,000,000.004 and not getting 1,000,000,000,000.0 as you would expect but getting something like 999,999,999,999,999. But you can get exactly the same problems when you create a double from a String, so having tested that my code gave reasonable results, I decided it would no worse than attempting to parse a String. And as noted, the String conversion becomes a real pain as soon as you have Exponential numbers.
Personally I think I would test to see if the double went beyond the limits of a long (Long.MAX_VALUE or Long.MIN_VALUE, and if not, do the conversion using a long. Its simple and you know you will never loose any digits of significance. If it is outside these bounds, then any errors introduced by incorrect values are going to be very small, so I think the % method will work.
But that is just me.....
10-30-2009 02:08 PM - edited 10-30-2009 02:09 PM
Any problems with this solution?
double round(double d)
{
return Math.floor(d + 0.5);
}
10-30-2009 02:38 PM
Amazing how you miss the obvious sometimes...
To match the requirements, would it not be
double round(double d)
{
return (Math.floor(d*100.0d + 0.5d)/100.0d;
}
10-30-2009 04:21 PM
Peter, sorry for the late reply, your earlier code sample done the job for me, i'm glad it got you thinking!
Thanks you so much to both of you for the input.
Regards
Alex
02-27-2010 09:32 AM
Hi,
I have the same issue with rounding a double.
This is my code :
Double discountedPrice1= new Double(priceFloat.floatValue()*(1-gauge1Float.floa
discountedPrice1=Math.floor(discountedPrice1);
When building I've got the error :
floor(double) in java.lang.Math cannot be applied to (java.lang.Double)
Can someone help me please ?
Thanks.
02-28-2010 05:48 AM - edited 02-28-2010 05:49 AM
The definition for Math.floor is
public static double floor(double a)
You are trying to apply this to a "Double". That is not the same as a "double". The Double class has a doubleValue method.