Jump to content
  • Advertisement
Sign in to follow this  
PureSnowX

Minor problem, rounding double.

This topic is 2350 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'v been trying all day rounding down a double to two decimals, without luck.
Currently I'm trying to do it with the BigDecimal like this:




public static double volume(double radius, double heigth) {
double value = Math.PI*radius*radius*heigth;
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(2, RoundingMode.UP);

value = bd.doubleValue();
return value;
}

Though the output is still x.xxxxxxxxex and not x.xx
I'v been trying to find other ways but they haven't worked either.

I'm sure it's an easy task but I can't for the life of me figure out what is making this NOT work.

Share this post


Link to post
Share on other sites
Advertisement

I'v been trying all day rounding down a double to two decimals, without luck.
Currently I'm trying to do it with the BigDecimal like this:




public static double volume(double radius, double heigth) {
double value = Math.PI*radius*radius*heigth;
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(2, RoundingMode.UP);

value = bd.doubleValue();
return value;
}

Though the output is still x.xxxxxxxxex and not x.xx
I'v been trying to find other ways but they haven't worked either.

I'm sure it's an easy task but I can't for the life of me figure out what is making this NOT work.


Yes, you are making it much harder then it is.


double newvalue = value * 100.0f + 0.5f;
int tempint = (int)newvalue;
double finalvalue = tempint/100.0f;


tada. That is it.

theTroll

Share this post


Link to post
Share on other sites
The problem is that a double can't hold 0.1 (or 0.01) exactly, so there will be a small rounding error for most return values (although doubles can store some values exactly - 0.5 for example).

If you need to do rounding I'd recommend doing so only when you output the value to the user. Keep the more precise value internally.

Share this post


Link to post
Share on other sites


double newvalue = value * 100.0f + 0.5f;
int tempint = (int)newvalue;
double finalvalue = tempint/100.0f;

Unfortunately some people seem to like to needlessly store an intermediate value in an int, preventing it from working on values over about 4 billion, which is actually a large portion of the possible values that a double can hold. It also does who knows what to infinities and NANs. And finally, convertion from floating point to integer is actually relatively slow.

The better way to do it is actually this:value = floor(value * 100 + 0.5) * 0.01;Hey presto, no problems with large numbers, and infinities and NANs are preserved. This also works with negative values since floor rounds towards minus infinity.

Having said that, most often when people ask this question what they actually want it their printing routines to output the rounded value rather than rounding the number that is actually stored in the variable. In that case the above is not sufficient, and in fact not even necessary. What you would actually want then is just:std::cout << std::fixed << std::setprecision(2) << value;

Share this post


Link to post
Share on other sites
It all works well up until somewhere about 100+
If I enter 150, then the output gets really crazy!


Output from my Sphere Volume method ( Yes they are named the same, that is part of the assignment):


public static double volume(int radius) {
double value = (4*Math.PI*radius*radius*radius)/3;

value = Math.floor(value* 100 + 0.5) * 0.001;
return value;
}

If "n" is equal to 150 then the output is this: 1.413716695E7

Share this post


Link to post
Share on other sites
Take a look at the line where you floor your value, there's a bug. Since this is an assignment I won't tell you what :P

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!