1

I have seen the solution to pad a number on each side of a decimal, in ruby, but not both on the same time.

here are the pieces I have:

mysize = 5000.127
f1_mysize = "%.2f" % mysize
puts f1_mysize

mysize2 = 5000.8
f2_mysize = "%06d" % mysize2
puts f2_mysize

I can't seem to put the two of those together, in one line. This number is a size, in GB, and I want to make sure the progress messages are lined evenly, so they are easier to read.

Can anyone help me with how to put those together?

Thanks!

4

1 回答 1

2

如果您要求,您可以两者都做:

'%08.2f' % 5.008

这里的符号是%0N.Mf其中N表示总共有多少位,M表示小数点后的位数。这是基于文档中解释printf的经典风格的参数。

于 2017-01-03T18:21:25.813 回答