保持数字长度不变

huangapple go评论83阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2016年2月29日 05:36:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/35688591.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定