问题描述
我知道在占位符的宽度之前放置任意数量的0
会实现零填充.例如,printf("%02d", 6);
打印06
.
I am aware that putting any number of 0
's before the width of the placeholder implements zero-padding. For example, printf("%02d", 6);
prints 06
.
但是将单个0
放在占位符的精度之前会做什么?例如,对于printf("%0.2lf", 0.123);
和printf("%.2lf", 0.123);
,输出均为0.12
.
But what does putting a single 0
before the precision of the placeholder do? For example, for both printf("%0.2lf", 0.123);
and printf("%.2lf", 0.123);
, the output is 0.12
.
如果什么都不做,是否有首选格式?
If it does nothing, is there a preferred format?
推荐答案
它们是等效的".如果您要使用%07.2",则将在前面添加额外的零将有所作为.
They are "equivalent". If you were to use "%07.2", then it would make a difference, by adding extra zeros on the front.
最初有%04.2",这当然没有任何区别,因为带有两个小数的浮点数始终总是4宽.
Originally had "%04.2", which of course doesn't make any difference, because a float with two decimals is always 4 wide anyway.
这篇关于作为printf占位符的%0.2lf和%.2lf之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!