Rounding Help
Author Message
ChrisHeritage

 Posted: Visual Basic Language, Rounding Help Top
 Hello, I'm new to the forums, so if I have placed this message in the wrong subject, I apologize. But, I'm hoping someone might be able to help me. I am trying to compare two numbers in VB.net. I want to compare all the digits to the left of the decimal point and only the first digit to the right to see if they match. For example: 8.64 and and 8.69 would be a match because all that I'm concerned with is the first digit to the right of the decimal point. But, if I use the Math.Round, it would round 8.64 to 8.6 and 8.69 to 8.7 - and then they wouldn't match. I thought I could determine the length of the number and then trim the digits, but there could any number of digits to the left and/or right of the decimal point. So, I don't know how I would accomplish that. Can anyone help Does anyone have any suggestions Thanks,Chris Heritage Visual Basic22

SJWhiteley

 Posted: Visual Basic Language, Rounding Help Top
 Use Math.Floor instead of math.round, after you multiply your numbers by 10.

spotty

 Posted: Visual Basic Language, Rounding Help Top
 Sure what your seeing is the effect known as bankers rounding. You could truncate the values and do something similar to the following Module Module1 Sub Main() Dim a As Single Dim b As Single a = System.Math.Round(8.64, 1) b = System.Math.Round(8.69, 1) Console.WriteLine(a.ToString) Console.WriteLine(b.ToString) a = (System.Math.Truncate(8.64 * 10) / 10) b = (System.Math.Truncate(8.69 * 10) / 10) Console.WriteLine(a.ToString) Console.WriteLine(b.ToString) End Sub End Module This would truncate to one decimal place. Obviously you could wrap this up in some function. I'm sure the framework must have something to handle this and will continue to search.

spotty

 Posted: Visual Basic Language, Rounding Help Top
 After looking at system.math.floor and system.math.ceiling - to get what we might consider normal rounding where if the last digit is a 5 or greater we round up and a 4 or less we round down then you may use something like the following. This may be what your looking for. Module Module1 Sub Main() Dim a As Single Dim b As Single a = System.Math.Round(8.64, 1) b = System.Math.Round(8.69, 1) Console.WriteLine(a.ToString) Console.WriteLine(b.ToString) a = (System.Math.Truncate(8.64 * 10) / 10) b = (System.Math.Truncate(8.69 * 10) / 10) Console.WriteLine(a.ToString) Console.WriteLine(b.ToString) 'DIFFERENT a = NormalRound(8.64) b = NormalRound(8.69) Console.WriteLine(a.ToString) Console.WriteLine(b.ToString) 'DIFFERENT a = NormalRound(8.64, 1) b = NormalRound(8.69, 1) Console.WriteLine(a.ToString) Console.WriteLine(b.ToString) 'SAME a = NormalRound(8.642, 2) b = NormalRound(8.644, 2) Console.WriteLine(a.ToString) Console.WriteLine(b.ToString) End Sub Function NormalRound(ByVal d As Double, Optional ByVal DecimalPlaces As Integer = 1) As Double Dim i As Integer = (10 ^ DecimalPlaces) Dim x As Integer x = d * i If x Mod i >= 5 Then Return System.Math.Floor(x) / i Else Return System.Math.Ceiling(x) / i End If End FunctionEnd Module

ChrisHeritage

 Posted: Visual Basic Language, Rounding Help Top
 Thank you for your help. Your suggestion worked perfectly. Thanks again. :)

John Lieurance

 Posted: Visual Basic Language, Rounding Help Top
 I also ran into this problem a while back and while my solution might not be the optimal it works. I found the trick is to compare the values as String types. Here's and example: Dim a, b As Decimal a = 8.64 b = 8.69 'this does not work If a.ToString("N1") = b.ToString("N1") Then MsgBox("The truncated numeric values match") End If 'you have to compare them as strings Dim c, d As String c = a.ToString d = b.ToString If c.Substring(1, c.IndexOf(".") + 1) = d.Substring(1, d.IndexOf(".") + 1) Then MsgBox("The truncated string values match") End If

SJWhiteley

 Posted: Visual Basic Language, Rounding Help Top
 Um, why doesn't a simple Math.Floor work since you don't want any rounding according to the origional post. Specifially, spotty, your function doesn't answer the origional question, which is to prevent rounding.

ChrisHeritage

 Posted: Visual Basic Language, Rounding Help Top
 Thanks everyone. SJWhiteley's solution seemed to be the easiest and that's what we used. Thanks again - it's nice to know there are people that will help out perfect strangers. :)

spotty

 Posted: Visual Basic Language, Rounding Help Top
 If your not rounding then your truncating. I'd shown a number of variations on a theme, truncating, rounding in a manner we are more accustomed to rather than the bankers rounding that is used by default which is often somewhat confusing to people at first what is occuring. All were shown in the examples. Pick which one is most applicable.