6

How do I check how many decimal places a number has in VB.NET?

For example: Inside a loop I have an if statement and in that statement I want to check if a number has four decimal places (8.9659).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lumart
  • 63
  • 1
  • 1
  • 3

7 Answers7

6

A similar approach that accounts for integer values.

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
vroc38
  • 61
  • 1
  • 1
5
Dim numberAsString As String = myNumber.ToString()
Dim indexOfDecimalPoint As Integer = numberAsString.IndexOf(".")
Dim numberOfDecimals As Integer = _
    numberAsString.Substring(indexOfDecimalPoint + 1).Length
Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
  • 1
    This isn't a good way to do this & would result in wildly inaccurate answers if the number had many digits to the right of the decimal, and its conversion to a string contained exponential notation. Try the above code with the following division equation: (1 / 11111111111111111). The string conversion is "9E-17", which means the resulting answer is 5 when it should be 17. One could of course extract the correct answer from the end of the string when the "E-" is present, but why do all that when it could be done mathematically instead? I'll post some code in an answer attached to this question. – spinjector Jan 27 '17 at 02:56
2
  Public Shared Function IsInSignificantDigits(val As Double, sigDigits As Integer)
    Dim intVal As Double = val * 10 ^ sigDigits
    Return intVal = Int(intVal)
   End Function
0

For globalizations ...

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
Wagner Pereira
  • 1,050
  • 11
  • 11
0

Some of the other answers attached to this question suggest converting the number to a string and then using the character position of the "dot" as the indicator of the number of decimal places. But this isn't a reliable way to do it & would result in wildly inaccurate answers if the number had many decimal places, and its conversion to a string contained exponential notation.

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

Here is a function I've just cooked up to do this. This isn't a perfect solution, and I haven't tested it thoroughly, 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's 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
spinjector
  • 3,121
  • 3
  • 26
  • 56
0

Just use the below in-built functions

Dim DecimalLength = StrReverse(number.ToString).IndexOf(".")
MsgBox(DecimalLength)
Hariprasath Yadav
  • 2,492
  • 1
  • 12
  • 15
-1

Simple...where n are the number of digits

Dim n as integer = 2
Dim d as decimal = 100.123456
d = Math.Round(d, n);
MessageBox.Show(d.ToString())

response: 100.12

AAP
  • 235
  • 1
  • 3
  • 9