22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/useful/ruby_extensions/numeric.rb', line 22
def pad_precision(num, opts = {})
opts[:precision] ||= 2
opts[:separator] ||= '.'
opts[:pad_number] ||= 0
num_s = num.to_s
num_s << opts[:separator] unless num_s.include?(opts[:separator])
ljust_count = num_s.split(opts[:separator])[0].length
ljust_count += (num_s.count(opts[:separator]) + opts[:precision].to_i) if opts[:precision] > 0
num_count = num.to_s.length
if ljust_count >= num_count
num_s.ljust(ljust_count, opts[:pad_number].to_s)
else
num_s[0..(ljust_count-1)]
end
end
|