# Minor problem, rounding double.

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

## 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 on other sites

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; 

theTroll

##### 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 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 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 on other sites
You realize that's scientific notation, right? 1.413716695 times 10 to the 7th.

##### Share on other sites
I did not realize that. But thank you! I'll note it.

##### 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

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 15
• 14
• 46
• 22
• ### Forum Statistics

• Total Topics
634054
• Total Posts
3015269
×