Decimal numbers in numbers in VB.NET

How to check the number of decimal places in VB.NET?

For example: Inside the loop, I have an if , and in this expression I want to check if the number has four decimal places (8.9659).

+4
source share
5 answers
 Dim numberAsString As String = myNumber.ToString() Dim indexOfDecimalPoint As Integer = numberAsString.IndexOf(".") Dim numberOfDecimals As Integer = _ numberAsString.Substring(indexOfDecimalPoint + 1).Length 
+5
source

A similar approach, taking integer values ​​into account.

 Public Function NumberOfDecimalPlaces(ByVal number As Double) As Integer Dim numberAsString As String = number.ToString() Dim indexOfDecimalPoint As Integer = numberAsString.IndexOf(".") If indexOfDecimalPoint = -1 Then ' No decimal point in number Return 0 Else Return numberAsString.Substring(indexOfDecimalPoint + 1).Length End If End Function 
+4
source
  Public Shared Function IsInSignificantDigits(val As Double, sigDigits As Integer) Dim intVal As Double = val * 10 ^ sigDigits Return intVal = Int(intVal) End Function 
+2
source

For globalization ...

 Public Function NumberOfDecimalPlaces(ByVal number As Double) As Integer Dim numberAsString As String = number.ToString(System.Globalization.CultureInfo.InvariantCulture) Dim indexOfDecimalPoint As Integer = numberAsString.IndexOf(".") If (indexOfDecimalPoint = -1) Then ' No decimal point in number Return 0 Else Return numberAsString.Substring(indexOfDecimalPoint + 1).Length End If End Function 
0
source

Some other answers related to this question involve converting a number into a string, and then using the character position of a β€œdot” as an indicator of the number of decimal places. But this is not a reliable way to do this and will lead to wildly inaccurate answers if the number was many decimal places, and its conversion to a string contained exponential notation.

For example, for equation 1/11111111111111111 (one divided by 17 units), the string conversion is "9E-17", which means that the resulting answer is 5 when it should be 17. Of course, you can extract the correct answer from the end of the line when present "E-", but why do all this when it could be done mathematically instead?

Here is the function I just prepared for this. This is not an ideal solution, and I have not tested it completely, but it seems to work.

 Public Function CountOfDecimalPlaces(ByVal inputNumber As Variant) As Integer ' ' This function returns the count of deciml places in a number using simple math and a loop. The ' input variable is of the Variant data type, so this function is versatile enougfh to work with ' any type of input number. ' CountOfDecimalPlaces = 0 'assign a default value of zero inputNumber = VBA.CDec(inputNumber) 'convert to Decimal for more working space inputNumber = inputNumber - VBA.Fix(inputNumber) 'discard the digits left of the decimal Do While inputNumber <> VBA.Int(inputNumber) 'when input = Int(input), it done CountOfDecimalPlaces = CountOfDecimalPlaces + 1 'do the counting inputNumber = inputNumber * 10 'move the decimal one place to the right Loop 'repeat until no decimal places left End Function 
0
source

All Articles