在vb.net中,我想提取wholenumber,decimal,前2個小數位和第3個和第4個小數位。我一直在圍繞解決方案進行盤旋,但並不是那樣。VB.net:顯示十進制值的某些部分
我到目前爲止的代碼是在這裏:
Dim wholenumber As Decimal
wholenumber = 15.1234
' Displays 15
MsgBox("The whole number is " & Math.Floor(wholenumber))
' Displays .1234
MsgBox("The decimals are " & wholenumber - Math.Floor(wholenumber))
' Displays .12
MsgBox("The first 2 decimals are" & ?????)
' Displays .0034
MsgBox("The third and fourth decimals are " & ????)
我最初開始與wholenumber.ToString,但發現它返回.119999999999和.0034000000000000096。對我而言,這不是一個太大的問題,但認爲他們可能是一個更好的方法。儘管如此,謝謝你的提示! – 2010-12-10 20:15:31
@ user538149:這是關於這種情況下的格式字符串。現在,如果您希望實際值被四捨五入,其他提供的答案將更有幫助。但是,如果您只是將_displaying_值指定爲小數位,那麼字符串格式化就是要走的路。 – David 2010-12-10 20:17:16
+1並啓用Option Strict,以便VB不會默默轉換,其危險。 – MarkJ 2010-12-11 08:27:58