JAMES
asked on
Rounding 0.125 to 0.13 !
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.
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.
if you multiply by 100 then use the ceil function and divide by 100 you should get what you want
ASKER
I have seen answers like that already but wont the ceiling ALWAYS round up!
I need 0.124 still to be 0.12.
I need 0.124 still to be 0.12.
Use decimal.Round(.0125,2)
It will give you the perfect answer 0.13
It will give you the perfect answer 0.13
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.
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.
ASKER
lucky_james,
With all respect - yuck! ;-)
With all respect - yuck! ;-)
You could always write your own rounding function that does exactly what you want.
check this:
double x = (double)0.125;
double y = Math.Round(x,2,MidpointRou nding.Away FromZero);
double x = (double)0.125;
double y = Math.Round(x,2,MidpointRou
did you try decimal.Round(.0125,2)
It will give you the perfect answer 0.13
It works fine for me.
It will give you the perfect answer 0.13
It works fine for me.
ASKER
vishwadh,
Not for me (this is in VS2003. I havent tried it in VS2005 yet).
decimal d = 0.125M;
MessageBox.Show(Decimal.Ro und(d,2).T oString()) ;
Gives 0.12
Not for me (this is in VS2003. I havent tried it in VS2005 yet).
decimal d = 0.125M;
MessageBox.Show(Decimal.Ro
Gives 0.12
ASKER
answer_me,
I dont think that overload is available in VS2003 (where this project currently is).
:-(
I dont think that overload is available in VS2003 (where this project currently is).
:-(
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
something like :
x = (0.125 *100) mod 1
if (x div 0.5 =0) then floor else ceil
x = (0.125 *100) mod 1
if (x div 0.5 =0) then floor else ceil
ASKER
answer_me,
What an interesting solution. I wonder why that works!!??
aschots,
Just about to test yours.
What an interesting solution. I wonder why that works!!??
aschots,
Just about to test yours.
ASKER
answer_me,
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.
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.