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

查看:21
本文介绍了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 位数字(只有其中的 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)?

推荐答案

你可以这样做:

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

6代表小数点后的位数.

6 represents the number of digits after the decimal separator.

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

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