Sha256: abb8e894ca2e5db29b8fa1f048f1bb4191c3dbe47a5fd9e70b2bfe2d08e0541a

Contents?: true

Size: 797 Bytes

Versions: 10

Compression:

Stored size: 797 Bytes

Contents

require 'bigdecimal'

module MoreCoreExtensions
  module DecimalSI
    DECIMAL_SUFFIXES = {"d" => "e-1", "c" => "e-2", "m" => "e-3", "μ" => "e-6", "n" => "e-9", "p" => "e-12", "f" => "e-15",
                        "a" => "e-18", "h" => "e2", "k" => "e3", "M" => "e6", "G" => "e9", "T" => "e12", "P" => "e15",
                        "E" => "e18"}.freeze

    def decimal_si_to_f
      Float(decimal_si_to_scientific_notation)
    end

    def decimal_si_to_big_decimal
      BigDecimal(decimal_si_to_scientific_notation)
    end

    private

    def decimal_si_to_scientific_notation
      multiplier = DECIMAL_SUFFIXES[self[-1]]
      if multiplier
        "#{self[0..-2]}#{multiplier}"
      else
        self
      end
    end
  end
end

String.send(:prepend, MoreCoreExtensions::DecimalSI)

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
more_core_extensions-4.5.0 lib/more_core_extensions/core_ext/string/decimal_suffix.rb
more_core_extensions-4.4.0 lib/more_core_extensions/core_ext/string/decimal_suffix.rb
more_core_extensions-4.3.1 lib/more_core_extensions/core_ext/string/decimal_suffix.rb
more_core_extensions-4.3.0 lib/more_core_extensions/core_ext/string/decimal_suffix.rb
more_core_extensions-4.2.0 lib/more_core_extensions/core_ext/string/decimal_suffix.rb
more_core_extensions-4.1.0 lib/more_core_extensions/core_ext/string/decimal_suffix.rb
more_core_extensions-4.0.0 lib/more_core_extensions/core_ext/string/decimal_suffix.rb
more_core_extensions-3.8.0 lib/more_core_extensions/core_ext/string/decimal_suffix.rb
more_core_extensions-3.7.0 lib/more_core_extensions/core_ext/string/decimal_suffix.rb
more_core_extensions-3.6.0 lib/more_core_extensions/core_ext/string/decimal_suffix.rb