Class: TurkishNumeric::TrNum

Inherits:
Object
  • Object
show all
Includes:
NumberPresenter
Defined in:
lib/turkish_numeric/tr_num.rb

Overview

Represents a number in Turkish language

Instance Method Summary collapse

Methods included from NumberPresenter

#text_non_spaced, #text_spaced, #thousands_separated, #to_pennies

Constructor Details

#initialize(number) ⇒ TurkishNumeric::TrNum

A new instance of TrNum



20
21
22
23
24
25
26
# File 'lib/turkish_numeric/tr_num.rb', line 20

def initialize(number)
  @sign = 'eksi' if number.negative?
  @decimal = Integer(number.abs)
  fraction_str = number.to_s.split('.')[1] || '0'
  @fraction_size = fraction_str.size
  @fraction = Integer(fraction_str, 10)
end

Instance Method Details

#to_money(symbol: '₺', thousand_sep: '.', penny_sep: ',') ⇒ String

Translate numeric value into currency notation



43
44
45
46
47
48
# File 'lib/turkish_numeric/tr_num.rb', line 43

def to_money(symbol: '₺', thousand_sep: '.', penny_sep: ',')
  [symbol,
   thousands_separated(decimal, sep: thousand_sep),
   penny_sep,
   to_pennies(fraction)].join
end

#to_money_text(currency: 'TL', sub_currency: 'kr') ⇒ String

Translate numeric value into text representaion of money



55
56
57
58
59
60
# File 'lib/turkish_numeric/tr_num.rb', line 55

def to_money_text(currency: 'TL', sub_currency: 'kr')
  pennies = to_pennies(fraction)
  decimal_part = decimal.zero? ? nil : [translate_partition(decimal), currency, ',']
  fractional_part = pennies&.zero? ? nil : [translate_partition(pennies), sub_currency]
  text_non_spaced(sign, decimal_part, fractional_part)
end

#to_textString

Translate numeric value into Turkish text



31
32
33
34
35
# File 'lib/turkish_numeric/tr_num.rb', line 31

def to_text
  decimal_part = translate_partition(decimal)
  fractional_part = fraction.zero? ? nil : [fractional_prefix, translate_partition(fraction)]
  text_spaced(sign, decimal_part, fractional_part)
end