Skip to content

Instantly share code, notes, and snippets.

@simonliu009
Created November 16, 2019 07:42
Show Gist options
  • Save simonliu009/a0a59e4bb7abcc04f7a56d70a0ce22f6 to your computer and use it in GitHub Desktop.
Save simonliu009/a0a59e4bb7abcc04f7a56d70a0ce22f6 to your computer and use it in GitHub Desktop.
[C保留指定位数小数]#c
本文章说明怎样使得你能保留小数点后几位(四舍五入)及其可控制的位数的实现:
#include <stdio.h>
int main()
{
double a = 423.43654;
a = int(a*100+0.5)/100.0; //到这的a值变成了423.440000
printf("%lf\n", a); //以小数后两位的形式输出
return 0;
}
运行结果:
423.44
负数的情况:
#include <stdio.h>
int main()
{
double a = -423.43654;
a = int(a*100-0.5)/100.0; //到这的a值变成了-423.440000
printf("%.2lf\n", a); //以小数后两位的形式输出
return 0;
}
运行结果:
-423.44
————————————————
版权声明:本文为CSDN博主「鹤影随行」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/hpf247/article/details/54090269
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment