使用 VB.net 查找小数点后的第一个非零数字
find first non-zero digit after decimal point using VB.net
我正在寻找小数点后第一个 none-零位数字的位置
到目前为止,我已经使用
找到了小数位数
Dim value As Single = 2.0533
Dim numberOfdecimaldigits As Integer = value.ToString().Substring(value.ToString().IndexOf(".") + 1).Length
MessageBox.Show(numberOfdecimaldigits)
如果我有 4.0342,那么我希望在小数点后 3 的位置得到 2。我想对这些数据做的是根据 none-0 数字的位置将整数加 2。例如:对于4.0342,我想让系统给它加上0.02。如果是 5.00784,那么我想给它加上 0.002。
有没有办法知道第一个none-小数点后零位的位置?
提前致谢
我强烈建议不要在此处使用字符串 — 您正在执行数值算法,使用数值逻辑处理数字更直接、更高效:
value = value - Math.Floor(value) ' Get rid of integer digits
Dim position = 0
While value > 0 AndAlso Math.Floor(value) = 0
value = value * 10
position += 1
End While
If value = 0 Then Throw New Exception(…)
Return position
这是我对防止无限循环的看法
value = value - Math.Floor(value) ' Get rid of integer digits
If value = 0 Then Throw New Exception(...)
value = 1/value ' Invert the value
Dim position = 0
While value > 1
value = value / 10
position += 1
End While
Return position
我正在寻找小数点后第一个 none-零位数字的位置
到目前为止,我已经使用
找到了小数位数Dim value As Single = 2.0533
Dim numberOfdecimaldigits As Integer = value.ToString().Substring(value.ToString().IndexOf(".") + 1).Length
MessageBox.Show(numberOfdecimaldigits)
如果我有 4.0342,那么我希望在小数点后 3 的位置得到 2。我想对这些数据做的是根据 none-0 数字的位置将整数加 2。例如:对于4.0342,我想让系统给它加上0.02。如果是 5.00784,那么我想给它加上 0.002。
有没有办法知道第一个none-小数点后零位的位置?
提前致谢
我强烈建议不要在此处使用字符串 — 您正在执行数值算法,使用数值逻辑处理数字更直接、更高效:
value = value - Math.Floor(value) ' Get rid of integer digits
Dim position = 0
While value > 0 AndAlso Math.Floor(value) = 0
value = value * 10
position += 1
End While
If value = 0 Then Throw New Exception(…)
Return position
这是我对防止无限循环的看法
value = value - Math.Floor(value) ' Get rid of integer digits
If value = 0 Then Throw New Exception(...)
value = 1/value ' Invert the value
Dim position = 0
While value > 1
value = value / 10
position += 1
End While
Return position