2011-12-12 43 views
-3

可能重複:
Simple division計算Pecentage在C#

我一卷想問問怎麼能代表這種數學表達式的C#

A/B * 100.0

我嘗試這樣做。但它不起作用,因爲百分比顯示這種價值(19)而不是(19.00);

int gradeA; 
int students; 
int percentage; 


percentage = (gradeA/students*100.0); 

percentLabel.Text=Convert.ToString(percentage); 

在此先感謝

+0

所以0/0 * 100爲19.00? –

回答

2
 //you want the values as a floating point: 
     double gradeA; 
     double students; 
     //ToString("P") displays the value as a percentage (e.g. 19%) 
     percentLabel.Text = (gradeA/students).ToString("P"); 
+0

thanks.it效果很好。 – Eppiey

1

那是因爲你的價值觀是integers,你需要一個float

嘗試:

float percentage; 

percentage = ((float)(gradeA/students))*100.0); 

percentLabel.Text = String.Format("{0.00}", percentage);