Sha256: b081032804af166271103296c05edcd543c0a37aeb48673fc646254d45a6b007

Contents?: true

Size: 1.85 KB

Versions: 3

Compression:

Stored size: 1.85 KB

Contents

require "attr_extras"

module Formatting
  module Number
    def format_number(number, opts = {})
      FormatNumber.new(number, opts).format
    end
  end

  class FormatNumber
    attr_private :input_number,
      :thousands_separator, :decimal_separator,
      :round, :min_decimals, :explicit_sign, :blank_when_zero

    def initialize(input_number, opts)
      @input_number = input_number

      @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)
    end

    def format
      number = input_number

      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(".")

      # Separate groups by thousands separator.
      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

3 entries across 3 versions & 1 rubygems

Version Path
formatting-0.0.13 lib/formatting/number.rb
formatting-0.0.12 lib/formatting/number.rb
formatting-0.0.11 lib/formatting/number.rb