lib/si/module.rb in si-0.1.2 vs lib/si/module.rb in si-0.1.3
- old
+ new
@@ -1,13 +1,16 @@
module SI
class << self
def convert num, options = {}
- options = { :length => options } if options.is_a?(Fixnum) && options >= 3
+ options = { :length => options } if options.is_a?(Fixnum)
options = DEFAULT.merge(options)
length,
min_exp,
max_exp = options.values_at(:length, :min_exp, :max_exp)
+ raise ArgumentError.new("Invalid length") if length < 2
+ return num.is_a?(Fixnum) ? '0' : "0.#{'0' * (length - 1)}" if num == 0
+
base = options[:base].to_f
minus = num < 0 ? '-' : ''
nump = num.abs
PREFIXES.keys.sort.reverse.select { |exp| (min_exp..max_exp).include? exp }.each do |exp|
@@ -25,10 +28,9 @@
nil
end
def revert str, options = {}
options = DEFAULT.select { |k, v| k == :base }.merge(options)
- base = options[:base]
pair = PREFIXES.to_a.find { |k, v| v == str[-1] }
if pair
str[0...-1].to_f * (options[:base] ** pair.first)
else