2013-12-03 65 views
-3

如果我想將3.32變成3.30和3.38變成3.40,我該怎麼做?我試過math.round(),但我做不到。如何在VB.NET中舍入到任意數量的小數(即從3.32到3.30)?

+0

http://stackoverflow.com/help/how-to-ask – Plutonix

+0

你嘗試這種之一[ ](http://stackoverflow.com/questions/14835001/what-is-wrong-with-math-round-in-vb-net)? – spajce

+2

你必須站在你的頭上,稱之爲「向上」。是的,使用Math.Round()。 –

回答

1

你可能在VB.NET尋找Math.Round Method

大紅大紫的值到最接近的整數或的分數 指定位數。

嘗試這樣的:

Math.Round(3.32, 1) 

或本:

Math.Round(3.32, 1, MidpointRounding.AwayFromZero) 
Math.Round(3.38, 1, MidpointRounding.AwayFromZero) 
+0

@HansPassant: - 我認爲OP錯過了指定他想要的大量數字,或者可能是MidpointRounding.AwayFromZero。但這只是一個猜測。 :) –

0

像這樣:

Math.Round(3.32, 1, MidpointRounding.AwayFromZero) ' Returns 3.3 
Math.Round(3.38, 1, MidpointRounding.AwayFromZero) ' Returns 3.4 

第一個參數是圓的數量。第二個參數指定在小數點後四捨五入的位數。第三個參數指定您希望使用標準的離零舍入而不是銀行家舍入)。

1

添加到以前的解決方案,讓兩位正確的十進制值,使用:

FormatNumber((Math.Round(3.32, 1, MidpointRounding.AwayFromZero)), 2) 
' Returns 3.30 

FormatNumber((Math.Round(3.38, 1, MidpointRounding.AwayFromZero)), 2) 
' Returns 3.40