Sha256: ca36682c2b845db196edae6b768bbc35faec97bb271bb3f3cb8f80bc279e79f8

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 KB

Contents

module Formatting
  module Number
    def format_number(number, opts = {})
      thousands_separator = opts.fetch(:thousands_separator) { default_thousands_separator }
      decimal_separator   = opts.fetch(:decimal_separator) { default_decimal_separator }
      round           = opts.fetch(:round, 2)
      min_decimals    = opts.fetch(:min_decimals, 2)
      explicit_sign   = opts.fetch(:explicit_sign, false)
      blank_when_zero = opts.fetch(:blank_when_zero, false)

      has_decimals = number.to_s.include?(".")

      if blank_when_zero
        return "" if number.zero?
      end

      # Avoid negative zero.
      number = 0 if number.zero?

      if round
        number = number.round(round) if has_decimals
      end

      integer, decimals = number.to_s.split(".")

      integer.gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{thousands_separator}")

      if explicit_sign
        integer = "+#{integer}" if number > 0
      end

      if min_decimals
        decimals ||= "0"
        decimals = decimals.ljust(min_decimals, "0")
      end

      [integer, decimals].compact.join(decimal_separator)
    end

    private

    def default_thousands_separator
      t_format(:delimiter, NON_BREAKING_SPACE)
    end

    def default_decimal_separator
      t_format(:separator, ".")
    end

    def t_format(key, default)
      if defined?(I18n)
        I18n.t(key, scope: "number.format", default: default)
      else
        default
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
formatting-0.0.9 lib/formatting/number.rb
formatting-0.0.8 lib/formatting/number.rb