Pad Ruby number on Left AND Right of Decimal?

133 views Asked by At

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!

1

There are 1 answers

1
tadman On BEST ANSWER

You can do both if you ask for it:

'%08.2f' % 5.008

The notation here is %0N.Mf where N represents how many places overall, and M for places after the decimal. This is based on the classic printf-style arguments explained in the documentation.