I am running VS 2003 and have applied SP1. (On WinXP SP2, .Net 1.1)
In the Command Window I get the following
? Math.Round(0.715, 2)
0.72
? Math.Round(0.725, 2)
0.72
? Math.Round(0.735, 2)
0.74
That is not a typo. ? Math.Round(0.725, 2) indeed returns 0.72.
This appears to be the reason I get a 1 cent rounding difference in certain
situations in my code.
The workaround I have implemented is of the form Math.Round(val + 1e-12,
places); however it's pretty ugly.
Help would be appreciated, thanks,
David. 10 15931
David Coleman wrote:
I am running VS 2003 and have applied SP1. (On WinXP SP2, .Net 1.1)
In the Command Window I get the following
? Math.Round(0.715, 2)
0.72
? Math.Round(0.725, 2)
0.72
? Math.Round(0.735, 2)
0.74
That is not a typo. ? Math.Round(0.725, 2) indeed returns 0.72.
This appears to be the reason I get a 1 cent rounding difference in certain
situations in my code.
The workaround I have implemented is of the form Math.Round(val + 1e-12,
places); however it's pretty ugly.
That is a classic floating point problem.
If it is a problem for you, then floating point is
probably not the correct data type.
Maybe you should use decimal ??
BTW, your workaround can create other problems if you numbers
can take full range.
Arne
David Coleman wrote:
I am running VS 2003 and have applied SP1. (On WinXP SP2, .Net 1.1)
In the Command Window I get the following
? Math.Round(0.715, 2)
0.72
? Math.Round(0.725, 2)
0.72
? Math.Round(0.735, 2)
0.74
That is not a typo. ? Math.Round(0.725, 2) indeed returns 0.72.
This appears to be the reason I get a 1 cent rounding difference in certain
situations in my code.
The workaround I have implemented is of the form Math.Round(val + 1e-12,
places); however it's pretty ugly.
Help would be appreciated, thanks,
David.
I think you want the MidpointRounding.AwayFromZero enum value.
If you always want to round 5 "up" (down if less than zero) then this is
what you want.
By default it appears to use MidpointRounding.ToEven which will round
towards the nearest even number.
Ie .115 will be rounded to .12, .125 will also be rounded to .12.
HTH
JB
John B wrote:
David Coleman wrote:
>I am running VS 2003 and have applied SP1. (On WinXP SP2, .Net 1.1)
In the Command Window I get the following
? Math.Round(0.715, 2) 0.72 ? Math.Round(0.725, 2) 0.72 ? Math.Round(0.735, 2) 0.74
That is not a typo. ? Math.Round(0.725, 2) indeed returns 0.72.
This appears to be the reason I get a 1 cent rounding difference in certain situations in my code.
The workaround I have implemented is of the form Math.Round(val + 1e-12, places); however it's pretty ugly.
Help would be appreciated, thanks,
David.
I think you want the MidpointRounding.AwayFromZero enum value.
If you always want to round 5 "up" (down if less than zero) then this is
what you want.
By default it appears to use MidpointRounding.ToEven which will round
towards the nearest even number.
Ie .115 will be rounded to .12, .125 will also be rounded to .12.
oops, Math.Round(value, decimalplaces, MidpointRounding.AwayFromZero) :)
>
HTH
JB
Thanks John B,
Explains the problem. But with VS 2003 Math.Round doesn't have a
MidpointRounding parameter overload.
All my business type calculations are done in static methods in calculations
classes so it's no big deal to do the fudge code for now.
Cheers,
David.
"John B" <jb******@yahoo.comwrote in message
news:45**********@news.iprimus.com.au...
John B wrote:
>David Coleman wrote:
>>I am running VS 2003 and have applied SP1. (On WinXP SP2, .Net 1.1)
In the Command Window I get the following
? Math.Round(0.715, 2) 0.72 ? Math.Round(0.725, 2) 0.72 ? Math.Round(0.735, 2) 0.74
That is not a typo. ? Math.Round(0.725, 2) indeed returns 0.72.
This appears to be the reason I get a 1 cent rounding difference in certain situations in my code.
The workaround I have implemented is of the form Math.Round(val + 1e-12, places); however it's pretty ugly.
Help would be appreciated, thanks,
David.
I think you want the MidpointRounding.AwayFromZero enum value. If you always want to round 5 "up" (down if less than zero) then this is what you want.
By default it appears to use MidpointRounding.ToEven which will round towards the nearest even number. Ie .115 will be rounded to .12, .125 will also be rounded to .12.
oops, Math.Round(value, decimalplaces, MidpointRounding.AwayFromZero) :)
>> HTH
JB
David Coleman wrote:
Thanks John B,
Explains the problem. But with VS 2003 Math.Round doesn't have a
MidpointRounding parameter overload.
All my business type calculations are done in static methods in calculations
classes so it's no big deal to do the fudge code for now.
Hmm, didnt know that about 1.1.
A "hack" suggested in a previous post http://groups.google.com.au/group/mi...4df858f6200f1e
is to call ToString("#.##") which does 5/4 rounding.
<...>
:(
JB
David Coleman <co******@hotmail.comwrote:
I am running VS 2003 and have applied SP1. (On WinXP SP2, .Net 1.1)
In the Command Window I get the following
? Math.Round(0.715, 2)
0.72
? Math.Round(0.725, 2)
0.72
? Math.Round(0.735, 2)
0.74
That is not a typo. ? Math.Round(0.725, 2) indeed returns 0.72.
Which is exactly what the documentation says it will do.
<quote>
Return Value
The number nearest value with a precision equal to digits. If value is
halfway between two numbers, one of which is even and the other odd,
then the even number is returned. If the precision of value is less
than digits, then value is returned unchanged.
</quote>
If you're using .NET 2.0 and want a different sort of rounding, specify
a MidpointRounding to use.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Another workaround:
Math.Ceiling((0.725 * 100)+.5)/100
----- Original Message -----
From: "David Coleman" <co******@hotmail.com>
Newsgroups: microsoft.public.dotnet.languages.csharp
Sent: Wednesday, September 20, 2006 5:25 AM
Subject: Math.Round problem
>I am running VS 2003 and have applied SP1. (On WinXP SP2, .Net 1.1)
In the Command Window I get the following
? Math.Round(0.715, 2)
0.72
? Math.Round(0.725, 2)
0.72
? Math.Round(0.735, 2)
0.74
That is not a typo. ? Math.Round(0.725, 2) indeed returns 0.72.
This appears to be the reason I get a 1 cent rounding difference in
certain situations in my code.
The workaround I have implemented is of the form Math.Round(val + 1e-12,
places); however it's pretty ugly.
Help would be appreciated, thanks,
David.
"David Coleman" <co******@hotmail.comwrote in message
news:ef**************@TK2MSFTNGP04.phx.gbl...
>I am running VS 2003 and have applied SP1. (On WinXP SP2, .Net 1.1)
In the Command Window I get the following
? Math.Round(0.715, 2)
0.72
? Math.Round(0.725, 2)
0.72
? Math.Round(0.735, 2)
0.74
That is not a typo. ? Math.Round(0.725, 2) indeed returns 0.72.
This appears to be the reason I get a 1 cent rounding difference in
certain situations in my code.
The workaround I have implemented is of the form Math.Round(val + 1e-12,
places); however it's pretty ugly.
This is known as banker's rounding. When working with rounding, always
rounding 0.5 up is not really fair. When dealing with money there would be a
bias. Compare the totals of your examples using the original numbers and the
rounded numbers. On the original total of 2.175 you want the total of the
rounded numbers to be 2.19 but using the examples you provided the total is
2.18.
If you have used the same rounding rules everywhere you should not have
rounding differences. However, you can't expect the total of rounded numbers
to equal the rounded total though. You will almost certainly have rounding
errors which is why you probably have a 1 cent difference. You need to
decide on a) what type of rounding you want and b) will you keep the rounded
figure or the original figure. If you keep the original figure then be
prepared for rounding errors like 0.03 + 0.03 + 0.03 = 0.10 (because 0.03
was really 0.034).
HTH
PS
I hope you mean Math.Floor (consider 0.726)
/claes
"Christof Nordiek" <cn@nospam.dewrote in message
news:uo**************@TK2MSFTNGP04.phx.gbl...
Another workaround:
Math.Ceiling((0.725 * 100)+.5)/100
----- Original Message -----
From: "David Coleman" <co******@hotmail.com>
Newsgroups: microsoft.public.dotnet.languages.csharp
Sent: Wednesday, September 20, 2006 5:25 AM
Subject: Math.Round problem
>>I am running VS 2003 and have applied SP1. (On WinXP SP2, .Net 1.1)
In the Command Window I get the following
? Math.Round(0.715, 2) 0.72 ? Math.Round(0.725, 2) 0.72 ? Math.Round(0.735, 2) 0.74
That is not a typo. ? Math.Round(0.725, 2) indeed returns 0.72.
This appears to be the reason I get a 1 cent rounding difference in certain situations in my code.
The workaround I have implemented is of the form Math.Round(val + 1e-12, places); however it's pretty ugly.
Help would be appreciated, thanks,
David.
Arne Vajhøj wrote:
David Coleman wrote:
>I am running VS 2003 and have applied SP1. (On WinXP SP2, .Net 1.1)
In the Command Window I get the following
? Math.Round(0.715, 2) 0.72 ? Math.Round(0.725, 2) 0.72 ? Math.Round(0.735, 2) 0.74
That is not a typo. ? Math.Round(0.725, 2) indeed returns 0.72.
This appears to be the reason I get a 1 cent rounding difference in certain situations in my code.
The workaround I have implemented is of the form Math.Round(val + 1e-12, places); however it's pretty ugly.
That is a classic floating point problem.
If it is a problem for you, then floating point is
probably not the correct data type.
Maybe you should use decimal ??
As other has pointed out, then the reason for the numbers
above being rounded as they are is the bankers rounding
(I did know that Round did bankers rounding).
But my point is still valid. You can not expect results that
make sense from a decimal number system perspective
when you use floating point.
Console.WriteLine(Math.Round(1.015, 2));
Console.WriteLine(Math.Round(2.015, 2));
Console.WriteLine(Math.Round(3.015, 2));
Console.WriteLine(Math.Round(4.015, 2));
outputs:
1,01
2,02
3,02
4,01
The correct solution is to use decimal. And specify the rounding
you want (I suspect that Round decimal is also using bankers
rounding by default).
Arne This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Tom |
last post by:
Has anyone ever seen a IComparer for floats the returns magnitude.
i.e. instead of returning -1, it would return -5. To let you know HOW
different the two numbers are. obviously for int it is a -...
|
by: Ark |
last post by:
Hi everyone,
Does anyone know if Direct3D overloads System.Math
functions?
Also is it possible to access the base functions of the
overloaded function (in other words restore original of
the...
|
by: Nicolas |
last post by:
Why does for the same code I got different result
the VB code gave me what I want why not the csharp?
Thank you for your help...
CSHARP CODE
int sx, sy;
double sdegrees;
sdegrees = (90 -...
|
by: ng_mr |
last post by:
No, not a question about "banker's rounding" or whatever it's called.
I want to round a double to the nearest 100th, so I perform the
following:
// original is a double
double result =...
|
by: Mitchell Vincent |
last post by:
Just making sure I'm not missing the boat here, but are there any
special routines for doing currency math (fixed precision stuff) in .NET?
The wonderful problems of doing math on decimals tend...
|
by: Chris Davoli |
last post by:
The folllowing will round to 526, but it should round to 527. It works
correctly for all other numbers, except for this one. Does anybody know of a
bug in Math.Round?
Dim ldecWater As Decimal =...
|
by: Altman |
last post by:
OK I was having rounding problems before and I didn't realize that
their was a third parameter in the round function that would tell it if
it a 5 to round up. I thought adding this would fix the...
|
by: Zeng |
last post by:
Math.Round has good behavior as following:
Math.Round(3.45, 1); //Returns 3.4. The last '5' is thrown away because 4 is
even
Math.Round(3.75, 1); //Returns 3.8. The last '5' is used because '7'...
|
by: =?Utf-8?B?UmVuZQ==?= |
last post by:
Hello everyone
I have a problem with Math.Round, it´s ocurring some strange:
Math.Round(12.985) = 12.98, it´s wrong. It should be: 12.99
Why?? What is the problem?
Help ME !!!!
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |