Sha256: b182366027bebad6696f55b8575a17d6d7cc75addd3fe9b1fa6a6ceebb1aef86

Contents?: true

Size: 783 Bytes

Versions: 1

Compression:

Stored size: 783 Bytes

Contents

require "numerology/version"

module Numerology
  def format_like(example)
    if example.gsub(/\D/,'') =~ /(0+$)/
      sig_fig = $1.length
      temp = self.round(sig_fig*-1)
    else
      temp = self
    end

    if example =~ /^[0\s]/
      formatted_string = "%#{example[0]}#{example.gsub(/\D/,'').length}d" % temp 
    else
      formatted_string = temp.to_s
    end

    formatted_string = separate_thousands(formatted_string, example[-4]) if example =~ /(\D)\d{3}$/

    formatted_string
  end

  def separate_thousands(formatted_string, separator)
    formatted_string =~ /([^\.]*)(\..*)?/
    int, dec = $1.reverse, $2 ? $2 : ""
    while int.gsub!(/(,|\.|^)(\d{3})(\d)/, '\1\2'+separator+'\3')
    end
    int.reverse + dec
  end
end

Numeric.send(:include, ::Numerology)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
numerology-0.0.3 lib/numerology.rb