英文:
Keeping numbers the same length
问题
我正在完成一个涉及时钟的示例,所以在某些情况下,我使用的是0到60之间的整数,而在其他情况下,使用的是0到12之间的整数。我需要每个数字的长度都是两位数,即使值小于10。
例如:
0 => 00
2 => 02
10 => 10
等等
我想要对它们进行格式化,但不一定要打印出来。最简单的方法是什么?
英文:
I'm completing an example involving a clock, so I'm working with integers between 0 and 60 in some cases, and 0 and 12 in others. I need the length of each number to be two digits long, even when the value is less than 10.
For example
0 => 00
2 => 02
10 => 10
etc
I want to have them formatted, but not necessarily to print them out. What's the easiest way to achieve this?
答案1
得分: 3
这可能是你想要的:
fmt.Printf("%02d", number)
查看 fmt
的文档以了解它的工作原理。
英文:
This is probably what you want:
fmt.Printf("%02d", number)
Have a look at fmt
's documentation to understand how it works.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论