Module: Num2words::Locales::ID

Defined in:
lib/num2words/locales/id.rb

Constant Summary collapse

ONES_MASC =
I18n.t("num2words.ones_masc", locale: :id)
ONES_FEM =
I18n.t("num2words.ones_fem", locale: :id)
TEENS =
I18n.t("num2words.teens", locale: :id)
TENS =
I18n.t("num2words.tens", locale: :id)
HUNDREDS =
I18n.t("num2words.hundreds", locale: :id)
SCALES =
I18n.t("num2words.scales", locale: :id)
FRACTIONS =
I18n.t("num2words.fractions", locale: :id)
GRAMMAR =
I18n.t("num2words.grammar", locale: :id)
DATE =
I18n.t("num2words.date", locale: :id)
DATE_TEMPLATE =
I18n.t("num2words.date.template", locale: :id)
TIME =
I18n.t("num2words.time", locale: :id)
TIME_TEMPLATE =
I18n.t("num2words.time.template", locale: :id)
DATETIME_TEMPLATE =
I18n.t("num2words.datetime.template", locale: :id)
ORDINALS =
I18n.t("num2words.numbers.ordinals", locale: :id)

Class Method Summary collapse

Class Method Details

.cardinal(number) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/num2words/locales/id.rb', line 80

def cardinal(number)
  integer_value = Integer(number)
  negative = integer_value.negative?
  integer_value = integer_value.abs

  return ONES_MASC[0] if integer_value.zero?

  groups = integer_value.to_s
                        .chars.reverse.each_slice(3).map(&:reverse)
                        .map(&:join).map!(&:to_i).reverse

  words = []
  groups.each_with_index do |group_value, index|
    next if group_value.zero?

    scale_idx = groups.size - index - 1
    words.concat triple_to_words(group_value, scale_idx)
  end

  words.unshift(minus_word) if negative
  words.join(" ")
end

.date_day(day, format:, date_case:) ⇒ Object



63
64
65
# File 'lib/num2words/locales/id.rb', line 63

def date_day(day, format:, date_case:)
  cardinal(day)
end

.date_year(year, format:) ⇒ Object



67
68
69
# File 'lib/num2words/locales/id.rb', line 67

def date_year(year, format:)
  cardinal(year)
end

.decimal_fraction_words(fraction_string) ⇒ Object



59
60
61
# File 'lib/num2words/locales/id.rb', line 59

def decimal_fraction_words(fraction_string)
  fraction_string.chars.map { |digit| cardinal(digit.to_i) }.join(" ")
end

.decimal_separator_wordObject



55
56
57
# File 'lib/num2words/locales/id.rb', line 55

def decimal_separator_word
  GRAMMAR[:conjunction]
end

.default_fraction_wordObject



47
48
49
# File 'lib/num2words/locales/id.rb', line 47

def default_fraction_word
  GRAMMAR[:default_fraction]
end

.fraction_joiner(joiner) ⇒ Object



43
44
45
# File 'lib/num2words/locales/id.rb', line 43

def fraction_joiner(joiner)
  joiner.to_sym == :and ? "dan" : GRAMMAR[:conjunction]
end

.fraction_numerator_feminine?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/num2words/locales/id.rb', line 51

def fraction_numerator_feminine?
  false
end

.integer_to_words(number, feminine: false) ⇒ Object



26
27
28
# File 'lib/num2words/locales/id.rb', line 26

def integer_to_words(number, feminine: false)
  cardinal(number)
end

.minus_wordObject



39
40
41
# File 'lib/num2words/locales/id.rb', line 39

def minus_word
  GRAMMAR[:minus]
end

.ordinal(value, format, gender: :masculine) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/num2words/locales/id.rb', line 71

def ordinal(value, format, gender: :masculine)
  ordinals = ORDINALS[format] || ORDINALS[:default] || ORDINALS[:nominative]
  gender_data = ordinals[gender] || ordinals[:masculine]

  return gender_data[value - 1] if value.between?(1, gender_data.length)

  cardinal(value)
end

.pluralize(_number, singular, _few, _plural) ⇒ Object



124
125
126
# File 'lib/num2words/locales/id.rb', line 124

def pluralize(_number, singular, _few, _plural)
  singular
end

.triple_to_words(number, scale_idx, feminine: false) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/num2words/locales/id.rb', line 30

def triple_to_words(number, scale_idx, feminine: false)
  return [] if number.zero?

  words = []
  words << under_thousand(number) unless scale_idx == 1 && number == 1
  words << SCALES[scale_idx][0] unless scale_idx.zero?
  words
end

.under_hundred(number) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/num2words/locales/id.rb', line 113

def under_hundred(number)
  return ONES_MASC[number] if number < 10
  return TEENS[number - 10] if number < 20

  tens = number / 10
  ones = number % 10
  words = ["#{ONES_MASC[tens]} puluh"]
  words << ONES_MASC[ones] if ones.positive?
  words.join(" ")
end

.under_thousand(number) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/num2words/locales/id.rb', line 103

def under_thousand(number)
  return under_hundred(number) if number < 100

  hundreds = number / 100
  rest = number % 100
  words = [hundreds == 1 ? "seratus" : "#{ONES_MASC[hundreds]} ratus"]
  words << under_hundred(rest) if rest.positive?
  words.join(" ")
end