本文介绍了作为printf占位符的%0.2lf和%.2lf之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在占位符的宽度之前放置任意数量的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之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 11:05