如何在打印时用零填充数字? [英] How to pad a number with zeros when printing?
本文介绍了如何在打印时用零填充数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何打印一个数字或用零填充字符串以使其宽度固定?
例如,如果我有数字 12
,我想让它 000012
。 解决方案
fmt包可以为您做到这一点:
<$ p $
$ f $ p $ f $ p $ $ b $ $ $ $ $ $ $ $
$ b
请注意%06d中的0,它将使其宽度为6并用零填充。第二个将填充空格。
你可以在这里看到它的动作: http://play.golang .org / p / cinDspMccp
How can I print a number or make a string with zero padding to make it fixed width?
For instance, if I have the number 12
and I want to make it 000012
.
解决方案
The fmt package can do this for you:
fmt.Printf("|%06d|%6d|\n", 12, 345)
Notice the 0 in %06d, that will make it a width of 6 and pad it with zeros. The second one will pad with spaces.
You can see it in action here: http://play.golang.org/p/cinDspMccp
这篇关于如何在打印时用零填充数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文