0

VB2010 ユーザーが数値形式を入力するユーザーフォームがあります。次に、ルーチンは数値ペアのリストを循環し、それらをカテゴリのリストに表示します。

 User format "0.00"
                            0.00 - 164.04
                            164.04 - 410.10
                            410.10 - 820.21

私がやろうとしているのは、最初の値を1桁増やして、重複がないようにすることです。何かのようなもの:

                            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))
4

2 に答える 2

0

アルゴリズムは次のとおりです。

1.小数点を見つける (p)

2.数値に10^pを掛け、1を増やし、10^pで割ります

Dim numAsStr As String = num.ToString()
Dim p As Integer = numAsStr.Length - numAsStr.IndexOf(".") - 1
Dim numInt as Integer = 10^p * num
Dim result as Double = ((10^p *num + 1.0) / 10^p).ToString()
于 2014-01-21T20:04:25.557 に答える