我正在做一道CS50题-Half. 我做了这道题,它还算管用,但例如,当我进入的时候:
Bill before tax and tip: 12.50
Sale Tax Percent: 8.875
Tip percent: 20
它的输出是$7.80
,而不是$8.17
.
进入时:
Bill before tax and tip: 100
Sale Tax Percent: 20
Tip percent: 10
它起作用了.
为什么给出小数是错误的,而给出四舍五入是有效的呢?
#include <cs50.h>
#include <stdio.h>
float half(float bill, float tax, int tip);
int main(void)
{
float bill_amount = get_float("Bill before tax and tip: ");
float tax_percent = get_float("Sale Tax Percent: ");
int tip_percent = get_int("Tip percent: ");
printf("You will owe $%.2f each!\n", half(bill_amount, tax_percent, tip_percent));
}
// TODO: Complete the function
float half(float bill, float tax, int tip)
{
float price;
tax = bill * tax / 100.0;
tip = (bill + tax) * tip / 100;
price = bill + tax + tip;
return price / 2;
}