Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 93
Registered: ‎10-22-2009
My Device: Not Specified
Accepted Solution

Round Double

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);
    }

 

Developer
Posts: 92
Registered: ‎06-02-2009
My Device: Rogers Curve 8900
My Carrier: Rogers

Re: Round Double

[ Edited ]

 

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; }

 

 

Developer
Posts: 19,633
Registered: ‎07-14-2008
My Device: Not Specified

Re: Round Double

[ Edited ]

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);
    }

Developer
Posts: 92
Registered: ‎06-02-2009
My Device: Rogers Curve 8900
My Carrier: Rogers

Re: Round Double

[ Edited ]

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.

Developer
Posts: 19,633
Registered: ‎07-14-2008
My Device: Not Specified

Re: Round Double

"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.....

Developer
Posts: 92
Registered: ‎06-02-2009
My Device: Rogers Curve 8900
My Carrier: Rogers

Re: Round Double

[ Edited ]

Any problems with this solution?

 

 

double round(double d)
{
   return Math.floor(d + 0.5);
}

 

 

Highlighted
Developer
Posts: 19,633
Registered: ‎07-14-2008
My Device: Not Specified

Re: Round Double

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;
}

Developer
Posts: 93
Registered: ‎10-22-2009
My Device: Not Specified

Re: Round Double

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

 

 

Developer
Posts: 275
Registered: ‎02-05-2010
My Device: Blackberry Z10
My Carrier: Bouygues Telecom

Re: Round Double

Hi,

I have the same issue with rounding a double.

This is my code :

 

Double discountedPrice1= new Double(priceFloat.floatValue()*(1-gauge1Float.floatValue()/100)/100+0.5); 

 

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.

Twitter : @LaurentKP
My apps
Developer
Posts: 562
Registered: ‎09-30-2009
My Device: Not Specified

Re: Round Double

[ Edited ]

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.