Posted at

桁数揃える

More than 5 years have passed since last update.

"%04d" % 20

# => "0020"

"%04d" % 2007
# => "2007"

power of ten

s = "3.070016013203525e-05"

f = s.to_f
# => 3.070016013203525e-05

"%0.10f" % f
# => "0.0000307002"

"%0.15f" % f
# => "0.000030700160132"

require "bigdecimal"
bd = BigDecimal(s)
# => #<BigDecimal:7fc325188518,'0.3070016013 203525E-4',27(27)>

bd.floor(2).to_f
# => 0.0

bd.floor(5).to_f
# => 3.0e-05

bd.floor(10).to_f
# => 3.07001e-05

bd.floor(15).to_f
# => 3.0700160132e-05