|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Question about rounding the decimalDear all,
In my vb.net program, I have to round a decimal number to two decimal place. For example, for the expression "Decimal.Round(31* 1.555, 2)", I should get the result "48.21". However, the program finally give "48.20". Is there any mistake for my expression? Please help. Thanks a lot, Ray "ray" <some***@microsoft.com> kirjoitti viestissä:udTIAvo9GHA.4***@TK2MSFTNGP02.phx.gbl...> Dear all, Quite interesting.> In my vb.net program, I have to round a decimal number to two decimal > place. > For example, for the expression "Decimal.Round(31* 1.555, 2)", I should > get the result "48.21". However, the program finally give "48.20". Is > there any mistake for my expression? Please help. > Thanks a lot, > Ray I can't say why it works like that but you could try Format(31*1.555,"0.00"). -Teemu Math.Round((31*1.555),2, MidpointRounding.AwayFromZero)
Thanks, Seth Rowe ray wrote: Show quoteHide quote > Dear all, > In my vb.net program, I have to round a decimal number to two decimal > place. > For example, for the expression "Decimal.Round(31* 1.555, 2)", I should > get the result "48.21". However, the program finally give "48.20". Is there > any mistake for my expression? Please help. > Thanks a lot, > Ray Dear all,
I have tried Math.Round((31*1.555),2, MidpointRounding.AwayFromZero) and the problem is solved. However, when I tried Math.Round(81.445, 2,MidpointRounding.AwayFromZero), I suppose that the program should give me 81.45 but actually it give me 81.44. Is there any mistake for my expression? Please help. Thanks a lot, Ray "rowe_newsgroups" <rowe_em***@yahoo.com> ???????:1161600302.947917.269***@f16g2000cwb.googlegroups.com...Show quoteHide quote > Math.Round((31*1.555),2, MidpointRounding.AwayFromZero) > > Thanks, > > Seth Rowe > > > ray wrote: >> Dear all, >> In my vb.net program, I have to round a decimal number to two decimal >> place. >> For example, for the expression "Decimal.Round(31* 1.555, 2)", I >> should >> get the result "48.21". However, the program finally give "48.20". Is >> there >> any mistake for my expression? Please help. >> Thanks a lot, >> Ray > That is very odd! It seems to not like .445 as the following all round
up: ..345 = .35 ..545 = .55 ..455 = .46 ..435 = .44 I have no idea why it's rounding down .445. The documentation states "The behavior of this method follows IEEE Standard 754, section 4" perhaps something in the IEEE documentation will explain it. I'll do some more research and let you know what I find. (Unless someone else wants to share :-) ) Thanks, Seth Rowe ray wrote: Show quoteHide quote > Dear all, > I have tried Math.Round((31*1.555),2, MidpointRounding.AwayFromZero) and > the problem is solved. > However, when I tried Math.Round(81.445, > 2,MidpointRounding.AwayFromZero), I suppose that the program should give me > 81.45 but actually it give me 81.44. Is there any mistake for my expression? > Please help. > Thanks a lot, > Ray > > "rowe_newsgroups" <rowe_em***@yahoo.com> > ???????:1161600302.947917.269***@f16g2000cwb.googlegroups.com... > > Math.Round((31*1.555),2, MidpointRounding.AwayFromZero) > > > > Thanks, > > > > Seth Rowe > > > > > > ray wrote: > >> Dear all, > >> In my vb.net program, I have to round a decimal number to two decimal > >> place. > >> For example, for the expression "Decimal.Round(31* 1.555, 2)", I > >> should > >> get the result "48.21". However, the program finally give "48.20". Is > >> there > >> any mistake for my expression? Please help. > >> Thanks a lot, > >> Ray > > rowe_newsgroups wrote:
Show quoteHide quote > That is very odd! It seems to not like .445 as the following all round It must be a issue with using doubles. When I use this code:> up: > > .345 = .35 > .545 = .55 > .455 = .46 > .435 = .44 > > I have no idea why it's rounding down .445. The documentation states > "The behavior of this method follows IEEE Standard 754, section 4" > perhaps something in the IEEE documentation will explain it. I'll do > some more research and let you know what I find. (Unless someone else > wants to share :-) ) > decimal d = 81.445m; MessageBox.Show(Math.Round(d,2,MidpointRounding.AwayFromZero).ToString()); It shows 81.45. But using a double shows 81.44 As always with floating point math, there are almost no values that can
be represented exactly. The closest value to 81.445 that can be represented is probably something like 81.444999999, which of course rounds to 81.44. ray wrote: Show quoteHide quote > Dear all, > I have tried Math.Round((31*1.555),2, MidpointRounding.AwayFromZero) and > the problem is solved. > However, when I tried Math.Round(81.445, > 2,MidpointRounding.AwayFromZero), I suppose that the program should give me > 81.45 but actually it give me 81.44. Is there any mistake for my expression? > Please help. > Thanks a lot, > Ray > > "rowe_newsgroups" <rowe_em***@yahoo.com> > ???????:1161600302.947917.269***@f16g2000cwb.googlegroups.com... >> Math.Round((31*1.555),2, MidpointRounding.AwayFromZero) >> >> Thanks, >> >> Seth Rowe >> >> >> ray wrote: >>> Dear all, >>> In my vb.net program, I have to round a decimal number to two decimal >>> place. >>> For example, for the expression "Decimal.Round(31* 1.555, 2)", I >>> should >>> get the result "48.21". However, the program finally give "48.20". Is >>> there >>> any mistake for my expression? Please help. >>> Thanks a lot, >>> Ray > > I just wish I knew how it calculated the value, and why it only rounds
down with .445. Any other double that ends with 45 (.345, .545, etc) rounds up. Any ideas on the math that goes on behind the scenes? Thanks, Seth Rowe Göran Andersson wrote: Show quoteHide quote > As always with floating point math, there are almost no values that can > be represented exactly. The closest value to 81.445 that can be > represented is probably something like 81.444999999, which of course > rounds to 81.44. > > ray wrote: > > Dear all, > > I have tried Math.Round((31*1.555),2, MidpointRounding.AwayFromZero) and > > the problem is solved. > > However, when I tried Math.Round(81.445, > > 2,MidpointRounding.AwayFromZero), I suppose that the program should give me > > 81.45 but actually it give me 81.44. Is there any mistake for my expression? > > Please help. > > Thanks a lot, > > Ray > > > > "rowe_newsgroups" <rowe_em***@yahoo.com> > > ???????:1161600302.947917.269***@f16g2000cwb.googlegroups.com... > >> Math.Round((31*1.555),2, MidpointRounding.AwayFromZero) > >> > >> Thanks, > >> > >> Seth Rowe > >> > >> > >> ray wrote: > >>> Dear all, > >>> In my vb.net program, I have to round a decimal number to two decimal > >>> place. > >>> For example, for the expression "Decimal.Round(31* 1.555, 2)", I > >>> should > >>> get the result "48.21". However, the program finally give "48.20". Is > >>> there > >>> any mistake for my expression? Please help. > >>> Thanks a lot, > >>> Ray > > > > Not exactly, but something like this:
The value 31 is represented as a floating point number as closely to 0.96875*2^5 as possible. The mantissa and exponent are stored binary, which means that 0.96875 is stored as 1/4 + 1/32 + 1/64 + 1/128 + 1/256 + 1/1024 + ... etc. 3.875 * 2^3 (I'm not sure if that is totally accurate, but you see how it works.) A Double value is accurate to 15 digits, so usually any value is off by something like 0.00000000000005%. So, even though 31 can easily be represented with total accuracy as an integer, it's very unlikely that it's stored as exactly 31 as a floating point number. rowe_newsgroups wrote: Show quoteHide quote > I just wish I knew how it calculated the value, and why it only rounds > down with .445. Any other double that ends with 45 (.345, .545, etc) > rounds up. Any ideas on the math that goes on behind the scenes? > > Thanks, > > Seth Rowe > > > Göran Andersson wrote: >> As always with floating point math, there are almost no values that can >> be represented exactly. The closest value to 81.445 that can be >> represented is probably something like 81.444999999, which of course >> rounds to 81.44. >> >> ray wrote: >>> Dear all, >>> I have tried Math.Round((31*1.555),2, MidpointRounding.AwayFromZero) and >>> the problem is solved. >>> However, when I tried Math.Round(81.445, >>> 2,MidpointRounding.AwayFromZero), I suppose that the program should give me >>> 81.45 but actually it give me 81.44. Is there any mistake for my expression? >>> Please help. >>> Thanks a lot, >>> Ray >>> >>> "rowe_newsgroups" <rowe_em***@yahoo.com> >>> ???????:1161600302.947917.269***@f16g2000cwb.googlegroups.com... >>>> Math.Round((31*1.555),2, MidpointRounding.AwayFromZero) >>>> >>>> Thanks, >>>> >>>> Seth Rowe >>>> >>>> >>>> ray wrote: >>>>> Dear all, >>>>> In my vb.net program, I have to round a decimal number to two decimal >>>>> place. >>>>> For example, for the expression "Decimal.Round(31* 1.555, 2)", I >>>>> should >>>>> get the result "48.21". However, the program finally give "48.20". Is >>>>> there >>>>> any mistake for my expression? Please help. >>>>> Thanks a lot, >>>>> Ray >>> > Oops... sent that one prematurely by mistake.
Göran Andersson wrote: Show quoteHide quote > Not exactly, but something like this: > > The value 31 is represented as a floating point number as closely to > 0.96875*2^5 as possible. The mantissa and exponent are stored binary, > which means that 0.96875 is stored as 1/4 + 1/32 + 1/64 + 1/128 + 1/256 > + 1/1024 + ... etc. > > 3.875 * 2^3 > > (I'm not sure if that is totally accurate, but you see how it works.) > > A Double value is accurate to 15 digits, so usually any value is off by > something like 0.00000000000005%. > > So, even though 31 can easily be represented with total accuracy as an > integer, it's very unlikely that it's stored as exactly 31 as a floating > point number. > Not exactly, but something like this:
The value 31 is represented as a floating point number as closely to 0.96875*2^5 as possible. The mantissa and exponent are stored binary, which means that 0.96875 is stored as 1/2 + 1/4 + 1/8 + 1/16 + ... etc. (I'm not sure if that is totally accurate, but you see how it works.) A Double value is accurate to 15 digits, so usually any value is off by something like 0.00000000000005%. So, even though a number like 31 can easily be represented with total accuracy as an integer, it's very unlikely that it's stored exactly as a floating point number. So it goes for every number and every calculation, so the final result is seldom exactly what you expect, but mostly so close that you never see the difference. It's only when you expect a value like 81.445 to be exactly 81.445 instead of something like 81.4449999999999, that you see the difference. rowe_newsgroups wrote: Show quoteHide quote > I just wish I knew how it calculated the value, and why it only rounds > down with .445. Any other double that ends with 45 (.345, .545, etc) > rounds up. Any ideas on the math that goes on behind the scenes? > > Thanks, > > Seth Rowe > > > Göran Andersson wrote: >> As always with floating point math, there are almost no values that can >> be represented exactly. The closest value to 81.445 that can be >> represented is probably something like 81.444999999, which of course >> rounds to 81.44. >> >> ray wrote: >>> Dear all, >>> I have tried Math.Round((31*1.555),2, MidpointRounding.AwayFromZero) and >>> the problem is solved. >>> However, when I tried Math.Round(81.445, >>> 2,MidpointRounding.AwayFromZero), I suppose that the program should give me >>> 81.45 but actually it give me 81.44. Is there any mistake for my expression? >>> Please help. >>> Thanks a lot, >>> Ray >>> >>> "rowe_newsgroups" <rowe_em***@yahoo.com> >>> ???????:1161600302.947917.269***@f16g2000cwb.googlegroups.com... >>>> Math.Round((31*1.555),2, MidpointRounding.AwayFromZero) >>>> >>>> Thanks, >>>> >>>> Seth Rowe >>>> >>>> >>>> ray wrote: >>>>> Dear all, >>>>> In my vb.net program, I have to round a decimal number to two decimal >>>>> place. >>>>> For example, for the expression "Decimal.Round(31* 1.555, 2)", I >>>>> should >>>>> get the result "48.21". However, the program finally give "48.20". Is >>>>> there >>>>> any mistake for my expression? Please help. >>>>> Thanks a lot, >>>>> Ray >>> > Wow, great explaination.
Thanks, Seth Rowe Göran Andersson wrote: Show quoteHide quote > Not exactly, but something like this: > > The value 31 is represented as a floating point number as closely to > 0.96875*2^5 as possible. The mantissa and exponent are stored binary, > which means that 0.96875 is stored as 1/2 + 1/4 + 1/8 + 1/16 + ... etc. > > (I'm not sure if that is totally accurate, but you see how it works.) > > A Double value is accurate to 15 digits, so usually any value is off by > something like 0.00000000000005%. > > So, even though a number like 31 can easily be represented with total > accuracy as an integer, it's very unlikely that it's stored exactly as a > floating point number. > > So it goes for every number and every calculation, so the final result > is seldom exactly what you expect, but mostly so close that you never > see the difference. > > It's only when you expect a value like 81.445 to be exactly 81.445 > instead of something like 81.4449999999999, that you see the difference. > > rowe_newsgroups wrote: > > I just wish I knew how it calculated the value, and why it only rounds > > down with .445. Any other double that ends with 45 (.345, .545, etc) > > rounds up. Any ideas on the math that goes on behind the scenes? > > > > Thanks, > > > > Seth Rowe > > > > > > Göran Andersson wrote: > >> As always with floating point math, there are almost no values that can > >> be represented exactly. The closest value to 81.445 that can be > >> represented is probably something like 81.444999999, which of course > >> rounds to 81.44. > >> > >> ray wrote: > >>> Dear all, > >>> I have tried Math.Round((31*1.555),2, MidpointRounding.AwayFromZero) and > >>> the problem is solved. > >>> However, when I tried Math.Round(81.445, > >>> 2,MidpointRounding.AwayFromZero), I suppose that the program should give me > >>> 81.45 but actually it give me 81.44. Is there any mistake for my expression? > >>> Please help. > >>> Thanks a lot, > >>> Ray > >>> > >>> "rowe_newsgroups" <rowe_em***@yahoo.com> > >>> ???????:1161600302.947917.269***@f16g2000cwb.googlegroups.com... > >>>> Math.Round((31*1.555),2, MidpointRounding.AwayFromZero) > >>>> > >>>> Thanks, > >>>> > >>>> Seth Rowe > >>>> > >>>> > >>>> ray wrote: > >>>>> Dear all, > >>>>> In my vb.net program, I have to round a decimal number to two decimal > >>>>> place. > >>>>> For example, for the expression "Decimal.Round(31* 1.555, 2)", I > >>>>> should > >>>>> get the result "48.21". However, the program finally give "48.20". Is > >>>>> there > >>>>> any mistake for my expression? Please help. > >>>>> Thanks a lot, > >>>>> Ray > >>> > > *** Sent via Developersdex http://www.developersdex.com ***
Hi,
I had the same problem with rounding.There is bug with Math.Round method in .NET. when u tried Math.Round(81.445,2,MidpointRounding.AwayFromZero),it will give you always 81.44. to get the value 81.45 but you will have to write seperate function for roundiing. for example: public static double RoundToTwoDecimalPlace(double dbValue,int Position) { decimal dc = (decimal)dbValue; System.Data.SqlTypes.SqlDecimal d = System.Data.SqlTypes.SqlDecimal.Round(dc, Position); //Math.Floor(db+.000005); dbValue = Convert.ToDouble(d.Value); return dbValue; } when you call this function it will round it from SQL DB type and it will give correct result. Try this method it will solve your problem. *** Sent via Developersdex http://www.developersdex.com *** ray wrote:
> Dear all, The precision is lost either in the calculation or the conversion. First > In my vb.net program, I have to round a decimal number to two decimal > place. > For example, for the expression "Decimal.Round(31* 1.555, 2)", I should > get the result "48.21". However, the program finally give "48.20". Is there > any mistake for my expression? Please help. > Thanks a lot, > Ray you multiply two Double values, which most likely results in a value that is not exact. Then you convert the Double value into Decimal, which also can introduce rounding errors.
object = nothing?
Need a numerical evaluator validate radio button in groupbox What is the event to trap when user clicks X to close form? How do I get back a newly created Primary key Compact .NET Framework on Desktop? What are the strings that define a connection's server version? Setting Null value in SQL update statement Get Special Folder Decision trees |
|||||||||||||||||||||||