module Formatters

Public Instance Methods

i18n_number(number_str, locale_spec) click to toggle source

Function creates localized version of a number represented by a string based on locale_spec eg. 123456.55 -> 123,45.55 (with locale_spec = {:separator : ',', :delimiter '.'}

5123456.55 -> 5 123 45,55 (with locale_spec = {:separator : ' ', :delimiter '.'}
# File lib/cuculungwa/formatters.rb, line 5
def i18n_number(number_str, locale_spec)
  splits = number_str.split('.')

  unless splits.length == 2
    fail Exception.new("#{number_str} doesn't contain dot.")
  end

  whole_part      = splits[0]
  fractional_part = splits[1]
  separator       = locale_spec[:separator]

  delimiter = unless fractional_part.empty?
                locale_spec[:delimiter]
              else
                ''
  end

  negative = false
  if whole_part[0] == '-'
    negative = true
    whole_part = whole_part[1..-1]
  end

  whole_part_result = ''
  for i in whole_part.length.downto(1)
    j = whole_part.length - i

    whole_part_result = separator + whole_part_result if j % 3 == 0 && j > 0

    whole_part_result = whole_part[i - 1] + whole_part_result
  end

  unless negative
    return whole_part_result + delimiter + fractional_part
  else
    return '-' + whole_part_result + delimiter + fractional_part
  end
end
pl_iban_formatter(number) click to toggle source
# File lib/cuculungwa/formatters.rb, line 56
def pl_iban_formatter(number)
  number = number.gsub(/\s+/, '').gsub('PL', '')
  number = number[0..1] + ' ' + number[2..-1].gsub(/(.{4})/, '\1 ').strip
  return number
end
short_iban(iban) click to toggle source
# File lib/cuculungwa/formatters.rb, line 44
def short_iban(iban)
  if iban.length >= 15
    first_part = iban[0..1] + ' ' + iban[2..3] + ' ' + iban[4..7]
    middle_part = ' ... '
    last_part = iban[-4..-1]
    iban_number = first_part + middle_part + last_part
  else
    iban_number = add_space = iban[0..1] + ' ' + iban[2..iban.length]
  end
  return iban_number
end