我有一些代码:
#include <math.h>
#include <stdio.h>
int main(void) {
int amount;
// user input for annual income
printf("Enter your amount: ");
scanf("%d", &amount);
if(amount <= 34000)
amount = amount * .33;
double new_amount = (double) amount;
printf("your calculated amount: %.2f\n", new_amout);
return 0;
}
我尝试接受一个整数值,然后在计算后将其转换为双精度值,但是当我输入int
时,它打印了一个小数,但没有非整数部分。
例如,如果我的金额是19522,我的代码将打印6442.00而不是6442.26。
1条答案
按热度按时间toe950271#
最简单的修复方法是将
amount
的数据类型更改为double
并更新格式字符串,删除new_amount
变量,因为它对您没有任何作用。另外,检查scanf()
是否成功,否则您可能正在操作未初始化的数据: