我尝试在 Stack Overflow 上搜索此内容,但找不到答案。
这是代码:
#include <stdio.h>
int main(void) {
double y;
printf("Enter a number: ");
scanf("%lf", &y);
printf("Your number when rounded is: %.2lf", y);
//If user inputs 5.05286, how can i round off this number so as to get the output as 5.00
//I want the output to be rounded as well as to be 2 decimal places like 10.6789 becomes 11.00
return 0;
}
我想对一个数字进行四舍五入,例如,如果数字是5.05286,则应四舍五入为5.00,如果是5.678901,则将其四舍五入到6.00小数2位。该数字5.678901正在四舍五入,5.05但它应该四舍五入5。我知道我可以使用floor()and ceil(),但我认为如果没有条件语句,我将无法完成答案,这不是我的C知识范围。我也尝试使用该round()功能,但它根本不圆。