【发布时间】:2014-01-21 19:55:47
【问题描述】:
VB2010 我有一个用户输入数字格式的用户表单。然后该例程循环遍历数字对列表并将它们显示在类别列表中:
User format "0.00"
0.00 - 164.04
164.04 - 410.10
410.10 - 820.21
我想要做的是将第一个值增加一个数字,这样就没有重叠。类似:
0.00 - 164.04
164.05 - 410.10
410.11 - 820.21
我正在尝试使它适用于用户输入的任何数字格式,例如“0.000”或“0.0”。我目前拥有的是(值 164.04 的示例)
1. Convert the value to a string "164.04"
2. Take the right most character "4" and convert to an integer 4
3. Increment the integer value by 1 to get 5
4. Take the characters in the string from step #1 except the last and then append
the integer from Step #3 as a string to get "164.05".
似乎可以在我的 VB6 程序中工作,但想看看是否有人有更好的想法。我也不认为我把最后一位数字解释为 9。
更新:根据以下建议,最终适用于正数和负数以及整数和浮点数的建议如下:
Dim p As Integer
Dim numAsStr As String = num.ToString(fmt)
If numAsStr.IndexOf(".") = -1 Then
p = 0
Else
p = numAsStr.Length - numAsStr.IndexOf(".") - 1
End If
Dim result as Double = ((num* (10 ^ p) + 1.0) / (10 ^ p))
【问题讨论】:
-
使用 numericupdown 并将系列 2 的最小值设置为比系列 1 的最大值大 0.01,以此类推
-
格式是用户输入的,所以它可以是从“0.0”到“0.000000”的任何值。
标签: vb.net numbers format increment significant-digits