431,745 Members | 1,897 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,745 IT Pros & Developers. It's quick & easy.

# I want to correct Round function

 P: n/a Hi, System.Math.Round function is confused me. for example i want to round 3.245 in with decimal symbol Result should be = 3.25 When i try to this in vb: A = 3.245 X = Round(A, 2) then x=3.24 , result is is false But when i try to A = 3.235 X = Round(A, 2) then x=3.24 , result is true So how can i trust this function. Or are there any true round function which you know? Not: Normally , I want to round with two decimal place (>=5 is round up, <5 is round down) Nov 21 '05 #1
17 Replies

 P: n/a Nomenklatura, I think you can trust the Round method to always act according to the documentation: "If value is halfway between two numbers, one of which is even and the other odd, then the even number is returned." "The behavior of this method follows IEEE Standard 754, section 4. This kind of rounding is sometimes called rounding to nearest, or banker's rounding." Kerry Moorman "nomenklatura" wrote: Hi, System.Math.Round function is confused me. for example i want to round 3.245 in with decimal symbol Result should be = 3.25 When i try to this in vb: A = 3.245 X = Round(A, 2) then x=3.24 , result is is false But when i try to A = 3.235 X = Round(A, 2) then x=3.24 , result is true So how can i trust this function. Or are there any true round function which you know? Not: Normally , I want to round with two decimal place (>=5 is round up, <5 is round down) Nov 21 '05 #2

 P: n/a Kerry, "If value is halfway between two numbers, one of which is even and the other odd, then the even number is returned." "The behavior of this method follows IEEE Standard 754, section 4. This kind of rounding is sometimes called rounding to nearest, or banker's rounding." I am curious where this is used beside banking. Is this standard used in your country and when yes, from what country are you? Cor Nov 21 '05 #3

 P: n/a "nomenklatura" wrote in message news:eV**************@TK2MSFTNGP10.phx.gbl... Hi, System.Math.Round function is confused me. for example i want to round 3.245 in with decimal symbol Result should be = 3.25 When i try to this in vb: A = 3.245 X = Round(A, 2) then x=3.24 , result is is false But when i try to A = 3.235 X = Round(A, 2) then x=3.24 , result is true So how can i trust this function. Or are there any true round function which you know? Not: Normally , I want to round with two decimal place (>=5 is round up, <5 is round down) In this instance, add 0.005 and truncate. I would suggest writing your own round function to handle various decimal places (e.g., MyRound, or whatever). Nov 21 '05 #4

 P: n/a "squig" wrote in message news:KN*******************@fe2.columbus.rr.com... "nomenklatura" wrote in message news:eV**************@TK2MSFTNGP10.phx.gbl... Hi, System.Math.Round function is confused me. for example i want to round 3.245 in with decimal symbol Result should be = 3.25 When i try to this in vb: A = 3.245 X = Round(A, 2) then x=3.24 , result is is false But when i try to A = 3.235 X = Round(A, 2) then x=3.24 , result is true So how can i trust this function. Or are there any true round function which you know? Not: Normally , I want to round with two decimal place (>=5 is round up, <5 is round down) In this instance, add 0.005 and truncate. I would suggest writing your own round function to handle various decimal places (e.g., MyRound, or whatever). One caveat -- be careful using FormatNumber since it returns a character string rather than numeric. If you have to do further computation with your (rounded) number, you will need to convert it back to a numeric. Nov 21 '05 #5

 P: n/a I am in Turkey. We pass new Currency unit at new year.. (TL->YTL). http://www.ytl.gen.tr/ytl/index_eng.php The Ministry of Finance want to round this format.. That is not important odd or even.. And still i couldn't use round function.. "Cor Ligthert" , iletide þunu yazdý news:uu**************@TK2MSFTNGP10.phx.gbl... Nov 21 '05 #6

 P: n/a nomenklatura, It appears that VS.NET 2005 (aka Whidbey, due out later in 2005) adds overloads to Math.Round to support current "banker's rounding" or the more conventional "round up". http://msdn2.microsoft.com/library/ef48waz8.aspx Otherwise as squig suggests you probably want to create your own rounding routine if "banker's rounding" is not appropriate for your routine... Hope this helps Jay "nomenklatura" wrote in message news:eV**************@TK2MSFTNGP10.phx.gbl... Hi, System.Math.Round function is confused me. for example i want to round 3.245 in with decimal symbol Result should be = 3.25 When i try to this in vb: A = 3.245 X = Round(A, 2) then x=3.24 , result is is false But when i try to A = 3.235 X = Round(A, 2) then x=3.24 , result is true So how can i trust this function. Or are there any true round function which you know? Not: Normally , I want to round with two decimal place (>=5 is round up, <5 is round down) Nov 21 '05 #7

 P: n/a i solved with FormatNumber functions thanks all Nov 21 '05 #8

 P: n/a "nomenklatura" schrieb: System.Math.Round function is confused me. In addition to the other replies: How To Implement Custom Rounding Procedures -- M S Herfried K. Wagner M V P V B Nov 21 '05 #9

 P: n/a That is round-toward-even and I wish everyone would use it (it is more accurate). Unfortunately, it is even harder than converting people to metric systems. :( Nov 21 '05 #10

 P: n/a "Randy Given" wrote in message news:73swd.6024\$sf5.840@lakeread05... That is round-toward-even and I wish everyone would use it (it is more accurate). Unfortunately, it is even harder than converting people to metric systems. :( Not trying to be argumentative but please explain how it is more accurate -- maybe I'm not fully understanding your comment. Nov 21 '05 #11

 P: n/a Squig, Why you do else think they call it "banking" system. The way that is always been used is alway for 4 figurs down(1,2,3,4) and for 5 up(5,6,7,8,9) That is eliminated in this system too an equal situations where it is 4,5 times down and 4,5 times up. However as Randy said it is hardly used. Probably because you need a computer to use it. Cor Nov 21 '05 #12

 P: n/a "Cor Ligthert" wrote in message news:OU**************@TK2MSFTNGP14.phx.gbl... Squig, Why you do else think they call it "banking" system. The way that is always been used is alway for 4 figurs down(1,2,3,4) and for 5 up(5,6,7,8,9) That is eliminated in this system too an equal situations where it is 4,5 times down and 4,5 times up. However as Randy said it is hardly used. Probably because you need a computer to use it. Cor Okay, but it still doesn't explain how it is more accurate. I understand the concept. Nov 21 '05 #13

 P: n/a Squig, Okay, but it still doesn't explain how it is more accurate. I understand the concept. A rounding is never accurate. However, in this newsgroup with so many persons whose native language is not English, it is used that you try to understand, not to pick on words. Just my thought, Cor Nov 21 '05 #14

 P: n/a Squig, The way I understand it is: Rather then always rounding the mid point up, "half of the time" you are rounding it up & "half of the time" you are rounding it down. The two "half of the time" together will average themselves out of the picture. When you always rounding the mid point up, then you start accumulating all the fractions, rather then average them out... The following demonstrates this: Public Shared Sub Main() Dim value1 As Decimal = 1.235D Dim value2 As Decimal = 1.245D Dim value3 As Decimal = value1 + value2 Dim value4 As Decimal = Decimal.Round(value1, 2) + Decimal.Round(value2, 2) Dim value5 As Decimal = RoundUp(value1, 2) + RoundUp(value2, 2) Debug.WriteLine(value1, "value1") Debug.WriteLine(value2, "value2") Debug.WriteLine(value3, "value3") Debug.WriteLine(value4, "value4") Debug.WriteLine(value5, "value5") End Sub ' may not handle negative value correctly... Private Shared Function RoundUp(ByVal value As Decimal, ByVal decimals As Integer) As Decimal decimals = CInt(10 ^ decimals) value *= decimals value = Decimal.Truncate(value + 0.5D) value /= decimals Return value End Function Notice how value4 & value5 are off by 1, if you are lot of rounding, this can add up significantly. When you use banking rounding, this difference will not add up as quickly or as much... Hope this helps Jay "squig" wrote in message news:X9****************@fe1.columbus.rr.com... "Randy Given" wrote in message news:73swd.6024\$sf5.840@lakeread05... That is round-toward-even and I wish everyone would use it (it is more accurate). Unfortunately, it is even harder than converting people to metric systems. :( Not trying to be argumentative but please explain how it is more accurate -- maybe I'm not fully understanding your comment. Nov 21 '05 #15

 P: n/a "Cor Ligthert" wrote in message news:#R**************@TK2MSFTNGP11.phx.gbl... Squig, Okay, but it still doesn't explain how it is more accurate. I understand the concept. A rounding is never accurate. However, in this newsgroup with so many persons whose native language is not English, it is used that you try to understand, not to pick on words. Just my thought, Cor Wasn't picking on words -- I'm trying to understand the reasoning behind it being more accurate. Nov 21 '05 #16

 P: n/a "Jay B. Harlow [MVP - Outlook]" wrote in message news:eG**************@TK2MSFTNGP10.phx.gbl... Squig, The way I understand it is: Rather then always rounding the mid point up, "half of the time" you are rounding it up & "half of the time" you are rounding it down. The two "half of the time" together will average themselves out of the picture. When you always rounding the mid point up, then you start accumulating all the fractions, rather then average them out... The following demonstrates this: Public Shared Sub Main() Dim value1 As Decimal = 1.235D Dim value2 As Decimal = 1.245D Dim value3 As Decimal = value1 + value2 Dim value4 As Decimal = Decimal.Round(value1, 2) + Decimal.Round(value2, 2) Dim value5 As Decimal = RoundUp(value1, 2) + RoundUp(value2, 2) Debug.WriteLine(value1, "value1") Debug.WriteLine(value2, "value2") Debug.WriteLine(value3, "value3") Debug.WriteLine(value4, "value4") Debug.WriteLine(value5, "value5") End Sub ' may not handle negative value correctly... Private Shared Function RoundUp(ByVal value As Decimal, ByVal decimals As Integer) As Decimal decimals = CInt(10 ^ decimals) value *= decimals value = Decimal.Truncate(value + 0.5D) value /= decimals Return value End Function Notice how value4 & value5 are off by 1, if you are lot of rounding, this can add up significantly. When you use banking rounding, this difference will not add up as quickly or as much... Hope this helps Jay Thanks for the explanation. The last sentence sums up why, which is what I was asking. Nov 21 '05 #17

 P: n/a > Not trying to be argumentative but please explain how it is more accurate -- maybe I'm not fully understanding your comment. If you have these numbers: 1.5 2.5 3.5 4.5 The real total is 12. Rounding up (typical way) gives 14: 1.5 -> 2 2.5 -> 3 3.5 -> 4 4.5 -> 5 Rounding toward even gives 12: 1.5 -> 2 2.5 -> 2 3.5 -> 4 4.5 -> 5 See? More accurate on average. Nov 21 '05 #18

### This discussion thread is closed

Replies have been disabled for this discussion. 