0

I'm working with VB.NET:

I'm looking for a function which gets the number of decimal places for a double value.

I already have a function which does this for values like "0.01", but not for values like "1E-6", but that's what I need.

There is a similar (answered) question on StackOverflow, but doesn't work for scientific notation: Decimal places in a number in VB.NET

Required function:

Input: 0.001 / Output: 3
Input: 0.000001 (.ToString() representation: "1E-6") / Output: 5

Have anyone an elegant way (VB.NET/C#)?

Community
  • 1
  • 1
user11909
  • 1,235
  • 11
  • 27

1 Answers1

0

I think this function should do it:

    Public Function GetDecimalPlaces(ByVal Value As Double, ByVal Culture As System.Globalization.CultureInfo) As Integer
        Dim val As String = String.Empty
        Dim valSplitted As String() = Nothing

        Try
            val = Value.ToString("0.######################")

            valSplitted = val.Split(CChar(Culture.NumberFormat.NumberDecimalSeparator))
            If (valSplitted.Length = 1) Then Return 0

            Return valSplitted(1).Length
        Catch ex As Exception
            Throw ex
        End Try
    End Function
user11909
  • 1,235
  • 11
  • 27