【问题标题】:Get decimal places for a double value获取双精度值的小数位
【发布时间】:2014-10-07 09:30:09
【问题描述】:
我正在使用 VB.NET:
我正在寻找一个获取双精度值小数位数的函数。
我已经有一个函数可以为“0.01”之类的值执行此操作,但不适用于“1E-6”之类的值,但这就是我需要的。
StackOverflow 上有一个类似(已回答)的问题,但不适用于科学记数法:
Decimal places in a number in VB.NET
所需功能:
Input: 0.001 / Output: 3
Input: 0.000001 (.ToString() representation: "1E-6") / Output: 5
有没有人优雅的方式(VB.NET/C#)?
【问题讨论】:
标签:
.net
double
scientific-notation
【解决方案1】:
我认为这个函数应该这样做:
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