我有另一个浮点输出问题打印“1.#R”,我已将其缩小到重复值,所以我的问题是如何将值四舍五入到小数点后 2 位,以便我可以打印该值(但不使用“ %.2f")。这是我的代码:
#include <stdio.h>
#include <stdlib.h>
void tax_deduction( float *total)
{
float taxpercent = 1.20;
float nationalInsurance = 1.175;
*total = *total - 9000;
*total = *total / taxpercent;
*total = *total / nationalInsurance;
*total = *total + 9000;
}
void hourly_rate(float *rate)
{
int weeks = 52;
float hours = 37.5;
*rate = *rate /(float)weeks;
*rate = *rate /hours;
}
int main()
{
float wages;
float hours = wages;
printf("Please enter your wage: \n");
scanf("%f",&wages);
if(wages > 9000){
tax_deduction(&wages);
}
hourly_rate(&hours);
printf("wages after tax and NI: %.2f\n", wages);
printf("wages per month: %.2f\n", wages / 12);
printf("hourly rate: %.2f\n", hours);
return 0;
}
它在运行 hourly_rate 函数后的显示方式存在问题,就像我在底部将其编码为printf("hourly rate: %.2f\n", (wages/52)/37/5); 它可以工作,但这并不是我真正想要的编码方式。
谁能想到为什么这不能正常工作?