如何隐藏在printf的前导零 [英] How to hide leading zero in 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
andF
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 the0
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屋!