質問

数値を切り捨てる必要があるVisual Basicアプリケーションがあります。たとえば、2.556は2.26ではなく2.55になります。

これを使用して、小数点から2つ以上の文字を取り除く機能を使用してこれを行うことができます。

Dim TheString As String
TheString = 2.556
Dim thelength = Len(TheString)
Dim thedecimal = InStr(TheString, ".", CompareMethod.Text)
Dim Characters = thelength - (thelength - thedecimal - 2)
_2DPRoundedDown = Left(TheString, Characters)

これを行うためのより良い機能はありますか?

役に立ちましたか?

解決

Math.Floor でこれを行うことができます。ただし、桁数を指定できないため、* 100を乗算して除算する必要があります

Dim theNumber as Double
theNumber = 2.556
Dim theRounded = Math.Sign(theNumber) * Math.Floor(Math.Abs(theNumber) * 100) / 100.0

他のヒント

String型の使用に依存しない別の方法:

Dim numberToRound As Decimal
Dim truncatedResult As Decimal
numberToRound = 2.556
truncatedResult = (Fix(numberToRound*100))/100

Math.Floor()の答えは良いです。 Fix()がどのVB環境で定義されているか正確にはわかりません。Justinが指摘しているように、Math.Floor()は負の数では動作しません。絶対値を取得してから、数値のSGN()を掛ける必要があります。数値のSiGN(sin()ではなく)を取得するために使用する関数の正確な名前はわかりません。

擬似コードでは、負の値を考慮に入れると、結果は次のようになります。

result = sgn( num ) * floor( abs( num * RoundToDig ) ) / RoundToDig

- 毛皮のような牛はmooして解凍します。

切り捨てる

Math.Floor(number)

文字をトリミングするには

number.Substring(0,1)

文字列に変換できます。

Dim Input As Decimal
Dim Output As Decimal
Input = 2.556
Output = Input - (Input Mod 0.01)

これは正数と負数の両方で機能します

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top