請問為什么我運行成功結果卻是69.400002 不應該是69.400000嗎?
#include <stdio.h>
float taxiFee(int n,int t)
{
??? float fee;
??? if((t<23)&&(t>=5))
??? {
?????? if(n<=3)
??? {
??????? fee = 13+1;
??? }
??? else
??? {
??????? fee = 13+1+(n-3)*2.3;
??? }
??? }
??? else
??? {
???????? if(n<=3)
??? {
??????? fee = 13+1;
??? }
??? else
??? {
??????? fee = 13+1+(n-3)*2.3*(1+0.2);
??? }
??? }
??? return fee;
}
int main()
{
??? float feeall;
??? feeall = taxiFee(12,9)+taxiFee(12,18);
??? printf("小明每天打車總費用為%f元",feeall);
}
2019-04-17
float型一般保留7位有效數字,%f默認情況下輸出6位小數,69.400000有效位數是8位,所以輸出的最后一位是無效的。所以要么printf("小明每天打車總費用為%.3f元",feeall);
要么把float型改成定義為double型,double型精度更高。
2019-04-17
#include <stdio.h>
static getMoney()
{
? ? double total;
? ? total=(13+(12-3)*2.3+1)*2;
? ? printf("打車費用%f\n",total);?
}
int main()
{
? ? getMoney();
? ? return 0;
}