我有一種方法,應該將十進制百分比轉換爲int 例如0,73應該返回73. 我做了下面的混亂,但想知道更好的解決方案。將十進制百分比轉換爲int
private static int ToPercentage(double d)
{
string temp = d.ToString("p");
string temp2 = temp.Replace("%", "");
double temp3 = Convert.ToDouble(temp2);
int result = (int) temp3;
return result;
}
return(int)(d * 100); –
將數值轉換爲另一種數值時,*爲什麼*會將中間步驟格式化爲字符串並返回? – Amy
這就像Code Golf的對面。 – Equalsk