C:printf的浮点值 [英] C: printf a float value

查看:203
本文介绍了C:printf的浮点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要打印的有2个整数位和逗号之后是6位小数浮点值。如果我只是用的printf(%F,myFloat)我得到一个截断的值。

I want to print a float value which has 2 integer digits and 6 decimal digits after the comma. If I just use printf("%f", myFloat) I'm getting a truncated value.

我不知道这是否总是用C发生,也只是因为我使用C微控制器(CCS是精确的)是,但在引用它告诉%F 得到这一点:截断浮动

I don't know if this always happens in C, or it's just because I'm using C for microcontrollers (CCS to be exact), but at the reference it tells that %f get just that: a truncated float.

如果我的持股量 44.556677 ,我打印出44.55,只有前两个小数位数

If my float is 44.556677, I'm printing out "44.55", only the first two decimal digits.

所以,问题是...我怎样才能打印我的6位数字(和他们的只是六,以防万一我有以后什么零)?

So the question is... how can I print my 6 digits (and just the six of them, just in case I'm having zeros after that or something)?

推荐答案

您可以做到这一点是这样的:

You can do it like this:

printf("%.6f", myFloat);

6重$ P $点后psents的位数。

6 represents the number of digits after the dot.

P.S。感谢@caf指正。

P.S. Thanks @caf for correcting me.

这篇关于C:printf的浮点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆