2014-03-25 39 views
-3

我有一個TextBoxString量,我想100將一個整數除以100得到兩個小數點的值?

劃分值應四捨五入至小數點後兩位。 我試過了,我沒有得到適當的價值。

這裏是我的嘗試:

6766/100 = 67.66 
47/100 = .47 
98/100 = .98 
+1

循環的輸入,因爲這兩個操作數是整數它做一個'int'計算。嘗試通過添加「.0」來將其中一個數字更改爲雙精度。 '6766/100.0' –

+0

http://stackoverflow.com/questions/22521867/float-doesnt-register-under-1/22521940#22521940 – heq

+1

@MatthewWatson你應該爲double添加'd',即'0d'。 – aevitas

回答

3

使用Math.Round。這個例子應該讓你去

string txtText = "78907"; 

double inputValue; 

if (double.TryParse(txtText, out inputValue)) 
    double result = Math.Round(inputValue/100, 2); 

輸出: 789.07

3

使用Math.Round。它有一個稱爲精度的參數。

例子:

Math.Round(1.23456, 2) -> 1.23 
3

使用Math.Round,但兩者之一必須是十進制類型,以避免整數除法:

double result = Math.Round(6766d/100, 2); 
0

數學。圓會做。

Math.Round(1.23456, 2); 

它將在2位小數

相關問題