我在C.如何確定數字中的小數位數?
寫一個程序是否有確定的浮動小數位數 的數量(即0.123 = 3
,0.123456 = 6
等)的方法嗎?
除了將數字轉換爲字符串和使用字符串 函數拉小數點後的字符?
我有一個名爲computeFare()函數
float computeFare()
{
totalFare = (3.40 + basicFare) * (1 + surchargePercent);
return totalFare; // **in this case totalFare = 34.567**
}
printf("Total fare is $%.2f\n",
computeFare());
結果:總票價爲$ 34.56
我做到這一點,它返回34.56 ... 我想這是$ 34.57
謝謝, 勞倫斯。
你想解決什麼問題? –
看看這個鏈接 http://stackoverflow.com/questions/1083304/cc-counting-the-number-of-decimals – 2012-09-04 12:41:26
浮動computeFare() { totalFare =(3.40 + basicFare)*(1 + surchargePercent ); return totalFare; //在這種情況下totalFare = 34.567 } 的printf( 「總票價是$%2F \ n」。, \t \t \t computeFare()); 我做了這個,它返回了34.56 ... 我希望它是$ 34.57 –