如何在ruby中将单个数字编号设为两位数?

时间:2021-07-28 23:58:55

Time.new.month returns a single digit representation of any month prior to October (e.g. June is 6), but I want a 2-digit format (i.e. instead of 6 I want 06).

Time.new.month返回10月之前任何月份的单个数字表示(例如6月6日),但我想要一个2位数格式(即代替6我想要06)。

I wrote the following solution, and I am asking to see some other/better solutions.

我写了以下解决方案,我要求看一些其他/更好的解决方案。

s = 6.to_s; s[1]=s[0]; s[0] = '0'; s #=> '06'

5 个解决方案

#1


25  

For your need I think the best is still

根据您的需要,我认为最好的仍然是

Time.strftime("%m")

as mentioned but for general use case the method I use is

如上所述,但对于一般用例,我使用的方法是

str = format('%02d', 4)
puts str

depending on the context I also use this one which does the same thing:

根据上下文我也使用这个做同样的事情:

str = '%02d %s %04d' % [4, "a string", 56]
puts str

Here is the documentation with all the supported formats: http://ruby-doc.org/core-2.3.1/Kernel.html#method-i-sprintf

以下是包含所有支持格式的文档:http://ruby-doc.org/core-2.3.1/Kernel.html#method-i-sprintf

#2


19  

You can use this printf-like syntax:

您可以使用这种类似printf的语法:

irb> '%02d' % 6
=> "06"

or

要么

str = '%02d' % 6

So:

所以:

s = '%02d' % Time.new.month
=> "06"

See the documentation for String#%.

请参阅String#%的文档。

#3


8  

Mat's answer pointing out that you can use the % operator is great, but I would like to point out another way of doing it, using the rjust method of the String class:

Mat的回答指出你可以使用%运算符很棒,但我想指出另一种方法,使用String类的rjust方法:

str = 6.to_s.rjust(2,'0')  # => "06"

#4


4  

If you're trying to format an entire date or date and time (rather than just the month), you might want to use strftime:

如果您尝试格式化整个日期或日期和时间(而不仅仅是月份),则可能需要使用strftime:

m = Time.now.strftime('%m')
# => "06"
t = Time.now.strftime('%Y-%m-%dT%H:%M:%SZ%z')
# => "2011-06-18T10:56:22Z-0700"

That will give you easy access to all the usual date and time formats.

这将使您可以轻松访问所有常用的日期和时间格式。

#5


2  

You can use sprintf:

你可以使用sprintf:

sprintf("%02d", s)

e.g. in irb:

例如in irb:

>> sprintf("%02d", s)
=> "06"

#1


25  

For your need I think the best is still

根据您的需要,我认为最好的仍然是

Time.strftime("%m")

as mentioned but for general use case the method I use is

如上所述,但对于一般用例,我使用的方法是

str = format('%02d', 4)
puts str

depending on the context I also use this one which does the same thing:

根据上下文我也使用这个做同样的事情:

str = '%02d %s %04d' % [4, "a string", 56]
puts str

Here is the documentation with all the supported formats: http://ruby-doc.org/core-2.3.1/Kernel.html#method-i-sprintf

以下是包含所有支持格式的文档:http://ruby-doc.org/core-2.3.1/Kernel.html#method-i-sprintf

#2


19  

You can use this printf-like syntax:

您可以使用这种类似printf的语法:

irb> '%02d' % 6
=> "06"

or

要么

str = '%02d' % 6

So:

所以:

s = '%02d' % Time.new.month
=> "06"

See the documentation for String#%.

请参阅String#%的文档。

#3


8  

Mat's answer pointing out that you can use the % operator is great, but I would like to point out another way of doing it, using the rjust method of the String class:

Mat的回答指出你可以使用%运算符很棒,但我想指出另一种方法,使用String类的rjust方法:

str = 6.to_s.rjust(2,'0')  # => "06"

#4


4  

If you're trying to format an entire date or date and time (rather than just the month), you might want to use strftime:

如果您尝试格式化整个日期或日期和时间(而不仅仅是月份),则可能需要使用strftime:

m = Time.now.strftime('%m')
# => "06"
t = Time.now.strftime('%Y-%m-%dT%H:%M:%SZ%z')
# => "2011-06-18T10:56:22Z-0700"

That will give you easy access to all the usual date and time formats.

这将使您可以轻松访问所有常用的日期和时间格式。

#5


2  

You can use sprintf:

你可以使用sprintf:

sprintf("%02d", s)

e.g. in irb:

例如in irb:

>> sprintf("%02d", s)
=> "06"