我正在为一个班级创建一种收银机程序,它需要一个购买金额和一个投标金额,然后给出剩余金额,然后将其分成每张账单的数量。问题是当我尝试存储浮点数时,它不会将数字存储在点的右侧。
这是我的代码:
int main(void)
{
float purchase_amount = 0.00f,
amount_tendered = 0.00f,
calculated_return = 0.00f;
float *p_return_amount;
printf("Welcome to Change Counter!\n\n");
printf("Please enter the total amount of purchase: ");
scanf("%f", &purchase_amount);
printf("\nPlease enter amount of money tendered: ");
scanf("%f", &amount_tendered);
// Debug output
printf("\n\nPurchase Amount: %.2f\nAmount Tendered: %.2f ", purchase_amount, amount_tendered);
calculated_return = amount_tendered - purchase_amount;
p_return_amount = &calculated_return;
printf("\n\nYour change is: %.2f", *p_return_amount);
system("pause");
return EXIT_SUCCESSFUL;
}
它只发生在非常大的数字上,而这正是我正在使用的。说 14990300.54 和 15000000.21 只存储 14990301.00 和 15000000.00。我出于某种原因自动四舍五入。
我的语法有问题吗?浮点数是否太小?
谢谢。