如何隐藏在printf的前导零 [英] How to hide leading zero in printf

查看:417
本文介绍了如何隐藏在printf的前导零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下输出 0.23 。我如何得到它简单地输出 0.23

 的printf(%8.2f0.23);


解决方案

C标准说,为˚F˚F浮点格式说明:


  

如果出现小数点字符,至少有一个数字出现之前。


我认为,如果你不想零到小数点前出现,你可能必须做一些像使用的snprintf()来格式化数字转换成字符串,并删除 0 如果格式化字符串开头0。 (以及类似地为-0)。然后传递格式化字符串我们的实际产出。或者类似的东西。

The following outputs 0.23. How do I get it to simply output .23?

printf( "%8.2f" , .23 );

解决方案

The C standard says that for the f and F floating point format specifiers:

If a decimal-point character appears, at least one digit appears before it.

I think that if you don't want a zero to appear before the decimal point, you'll probably have to do something like use snprintf() to format the number into a string, and remove the 0 if the formatted string starts with "0." (and similarly for "-0."). Then pass that formatted string to our real output. Or something like that.

这篇关于如何隐藏在printf的前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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