[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
C语言中的格式化打印 :
* 例如: printf("%.2f",21.195); 输出是 21.20 四舍五入保留了
* 定义宏变量 #define 即符号常量 也能够四舍五入保留
而变量和常变量 并不四舍五入:
float a=21.195 ; const float b=21.195; printf("%.2f \n %.2f",a,b); 输出为 21.19
并没有四舍五入,而是直接截断了
* 变量和常变量 不四舍五入
* 宏定义符号常量和实型常量 要四舍五入
今天做一个很简单的实验的时候,测试用例有一个一直有问题,便揣测应该是精度问题:
先看题目:
正常coding:
#include<stdio.h> #define PI 3.14 int main(void) { /*********Begin*********/
float r, h; //输入半径和高 scanf("%f,%f", &r, &h); float c, s, v; c = 2 * r * PI; s =
4*PI*r*r; v = PI*r*r*h; int x = round(v); printf("C1=%.2f\n", c); printf(
"Sb=%.2f\n", s); printf("Vb=%.2f\n", v); /*********End**********/ return 0; }
然后发现测试有问题:
题目要求通过的是 最后一个 Vb体积为 21.20 而PI是确定的3.14
那问题出在哪了呢?
于是我便不保留2位输出 把 “%.2f” 换为 “%f”
所以可知 C语言中的 格式化打印 %.2f 保留两位 对于变量和常变量 是直接割舍的 不是四舍五入
测试了Java的格式化输出 Java中的是四舍五入的
可以看到 Java中不管是实型常量、常变量还是变量输出都是四舍五入的
那要怎么解决四舍五入保留小数呢?
* 方案一:
在计算体积的时候采用:添加这样一个方法来
v=(int)(v*100+0.5)/100.0;
* 先将体积扩大100倍,
* +0.5再把float类型强转为int类型即可达到四舍五入的效果,
* 再除以100.0浮点数又自动转为浮点类型 #include<stdio.h> #include<math.h> #define PI 3.14 int
main(void) { /*********Begin*********/ float r, h; scanf("%f,%f", &r, &h); float
c, s, v; c = 2 * r * PI; s = 4*PI*r*r; v = PI*r*r*h; v=(int)(v*100+0.5)/100.0;
int x = round(v); printf("C1=%.2f\n", c); printf("Sb=%.2f\n", s); printf(
"Vb=%.2f\n", v); /*********End**********/ return 0; }
* 方案二: #include<math.h> 里提供了这么一个方法: round() 用例: float a=3.1415926; float b=
round(a); 可得到 b=3
所以还是和方案一的思路一样
加一个:
v=round(v*10)/10.0;
即可,效果一样
总结一下,C语言中的格式化打印输出,对于实型常量和符号常量是能够四舍五入保留的,但对于变量和常变量却不行,但自己处理。