Solved

# Rounding 0.125 to 0.13 !

Posted on 2007-10-17
363 Views
Hi,

I am using the Math.Round function to round my values to two decimal places but when I use the value 0.125 it gets rounded to 0.12 and not 0.13 which is what I require.

This adheres to the docs quite happily but not what I want!

"The number nearest d with precision equal to decimals. If d is halfway between two numbers, one of which is even and the other odd, then the even number is returned. If the precision of d is less than decimals, then d is returned unchanged."

I have also tried Decimal.Round to no avail.

Any thoughts?

Many thanks.

James.
0
Question by:JAMES

Expert Comment

if you multiply by 100 then use the ceil function and divide by 100 you should get what you want
0

Author Comment

I have seen answers like that already but wont the ceiling ALWAYS round up!

I need 0.124 still to be 0.12.
0

Expert Comment

Use decimal.Round(.0125,2)

It will give you the perfect answer 0.13
0

LVL 7

Expert Comment

if the ceiling function is not helping you then perhaps you need to code your own function.

change it into a string.
loop into it and start from back.
Inside loop you can convert the currently pointed character to int

here yop can put your own custom conditions into th loop.

Hope this helps.
0

Author Comment

lucky_james,

With all respect - yuck! ;-)
0

Expert Comment

You could always write your own rounding function that does exactly what you want.
0

LVL 10

Expert Comment

check this:
double x = (double)0.125;
double y = Math.Round(x,2,MidpointRounding.AwayFromZero);
0

Expert Comment

did you try decimal.Round(.0125,2)

It will give you the perfect answer 0.13

It works fine for me.
0

Author Comment

Not for me (this is in VS2003.  I havent tried it in VS2005 yet).

decimal d = 0.125M;
MessageBox.Show(Decimal.Round(d,2).ToString());

Gives 0.12
0

Author Comment

I dont think that overload is available in VS2003 (where this project currently is).

:-(
0

LVL 10

Accepted Solution

use this in 2003 for two decimal places
decimal x=0.125M;
string str= x.ToString("#.##");
0

Expert Comment

something like :
x = (0.125 *100) mod 1
if (x div 0.5 =0) then floor else ceil
0

Author Comment

What an interesting solution.  I wonder why that works!!??

aschots,

0

LVL 10

Expert Comment

0

Author Comment

Great stuff - well deserved points.

aschots - thanks for your help throughout but im going with answer_me's answer - just feels "cleaner" in a dirty sort of way!

Thanks all.

James.
0

## Featured Post

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…