更改double值的格式并返回double的结果 [英] Change format of double value and return result as double
本文介绍了更改double值的格式并返回double的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
我正在寻找一种方法来改变c语言中双值的格式,就像c#中的以下命令一样:
Hi there,
I'm looking for a way to change format of my double value in c language like how the following command do in c#:
Math.Round(MyDoubleValue, 1);
所以它是圆形的,也可以截断到一个数字后浮点数。
但是我在我的搜索中找到的唯一的东西就像下面这样:
So it is rounded and also truncated to just one digit as float after dot.
But the only thing that i could find in my lots of searches was something like below :
sprintf("%.1f", MyDoubleValue);
它打印的确切是我想要的,但问题是我不想打印结果而且我想将它设置为另一个双变量。
有什么办法吗?
It prints exactly what i want, however the problem is that i do not want to print the result but also i want to set it to another double variable.
Is there any way?
推荐答案
check 舍入数字为C中的2个小数位数 [ ^ ]
check Rounding Number to 2 Decimal Places in C[^]
#include <math.h>
float val = 37.777779;
float rounded_down = floorf(val * 10) / 10; /* Result: 37.7 */
float nearest = roundf(val * 10) / 10; /* Result: 37.8 */
float rounded_up = ceilf(val * 10) / 10; /* Result: 37.8 */</math.h>
标准C没有舍入函数:它确实有底线:
Standard C doesn't have a rounding function: it does have floor however:
float f = 12.3456;
float r = floor((f * 10.0) + 0.5) / 10.0;
应该这样做。
这篇关于更改double值的格式并返回double的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文