#include<stdio.h>
#include<math.h>
int main()
{
int id;
float c,math,english;
scanf("%d;%f,%f,%f",&id,&c,&math,&english);
c=round(c*100)/100;
math=round(math*100)/100;
english=round(english*100)/100;
printf("The each subject score of No. %d is %.2f, %.2f, %.2f.",id,c,math,english);
return 0;
}
光使用%.2f的形式保留两位有效数字是"银行家舍入法(四舍六入五成双)"而不是我i们平时定义的四舍五入,当遇到21.192的情况是第二位9是奇数就不会进一,所以我们使用round()函数进行严格的四舍五入.
- 引入了 <math.h> 头文件,以便使用 round() 函数
- 四舍五入的原理:将数值乘以 100,把需要保留的两位小数移到整数部分
使用 round() 函数对整数部分进行四舍五入
再除以 100,恢复到原有的数量级
注:保留几位有效数字就乘10的几次方并除.

京公网安备 11010502036488号