num_words 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a0d117daa9a0ef678984f1a03b1b1456047e7ea67b4b13f610ab8cfb90634646
4
- data.tar.gz: 4732ef7b105d1ae22b208529fe6098cbab124aef3547ee4650f3d24dd4b3a89e
3
+ metadata.gz: c707e6c57aaf783107cb3290b903a117e012ce6c187d01eb9c6845c709c247a3
4
+ data.tar.gz: 8cde67422c55d5ebc89d90a70992af08a5f8404cdc121b250adf901e5ded661d
5
5
  SHA512:
6
- metadata.gz: 75c3b9d42dc93e10d5a413d2f1a03b04921f8277dd1ae056bcf4f020c9672e6e4737752d82d02541e9ac9b0560e0facb5888e64cb3de72ee83dcb881855f27b0
7
- data.tar.gz: 666c1624f2d5c82ac29cd479f3c1625f0f43941be7876d190b3483ff87de195d4c949b9d88b74b9c10e8886f64e2657d91812ce85a2041fd5705d4038900208c
6
+ metadata.gz: f7d41d25b31437a63ac7ae7efd4e91c9ae930fc52b087f5c83951d28a4accf70b8e5c1bb88019822630afdb5a846da9406f81ee99c9564760a0f90b60afd4961
7
+ data.tar.gz: 2d94b537a8db62c48749bb0ae149efb58d91c976e334fb0aec48f1e9649b6146b5d89671d9ef1f8b063ee8144ea53086166f98e7a2a7ce2c3391ea5cc6183fea
data/README.md CHANGED
@@ -9,9 +9,6 @@
9
9
  - Convert integers and decimals to words.
10
10
  - Supports multiple country numbering systems:
11
11
  - **US / American** – `thousand, million, billion`
12
- - **European** – `thousand, million, milliard, billion`
13
- - **UK** – `thousand, million, billion`
14
- - **France** – `mille, million, milliard`
15
12
  - **India** – `thousand, lakh, crore`
16
13
  - Supports multiple languages:
17
14
  - **English** (`:en`)
@@ -20,13 +20,16 @@ module NumWords
20
20
  90=>"नब्बे",91=>"इक्यानवे",92=>"बयानवे",93=>"तिरेनवे",94=>"चौरानवे",95=>"पचानवे",96=>"छियानवे",97=>"सत्तानवे",98=>"अट्ठानवे",99=>"निन्यानवे"
21
21
  }.freeze
22
22
 
23
+ # --- Units ---
23
24
  INDIAN_UNITS_HI = ['', 'हज़ार', 'लाख', 'करोड़', 'अरब', 'खरब'].freeze
25
+ INDIAN_UNITS_EN = ['', 'thousand', 'lakh', 'crore', 'arab', 'kharab'].freeze
26
+
27
+ # --- English exponents ---
24
28
  AM_EXPONENTS = {
25
29
  3 => 'thousand', 6 => 'million', 9 => 'billion', 12 => 'trillion',
26
30
  15 => 'quadrillion', 18 => 'quintillion', 21 => 'sexillion', 24 => 'septillion',
27
31
  27 => 'octillion', 30 => 'nonillion', 33 => 'decillion', 36 => 'undecillion'
28
32
  }.freeze
29
- INDIAN_UNITS_EN = ['', 'thousand', 'lakh', 'crore', 'arab', 'kharab'].freeze
30
33
 
31
34
  COUNTRY_EXPONENTS = {
32
35
  us: AM_EXPONENTS,
@@ -35,30 +38,26 @@ module NumWords
35
38
 
36
39
  class << self
37
40
  # Convert number to words
41
+ # Default language: :en
38
42
  def to_words(number, country: :us, language: :en, include_and: true)
39
- if [:in].include?(country) && language == :hi
40
- return to_words_indian(number)
41
- end
42
-
43
43
  val = number.to_i.abs
44
- return 'zero' if val.zero?
44
+ return language == :hi ? HINDI_NUMBERS_1_TO_99[0] : 'zero' if val.zero?
45
+
46
+ if country == :in
47
+ return language == :hi ? to_words_indian(number) : to_words_indian(number, language: :en)
48
+ end
45
49
 
46
50
  exp_hash = COUNTRY_EXPONENTS[country] || AM_EXPONENTS
47
51
  ones_array = language == :hi ? HINDI_NUMBERS_1_TO_99 : ONES_EN
48
52
  tens_array = language == :hi ? HINDI_NUMBERS_1_TO_99 : TENS_EN
49
- units = language == :hi ? INDIAN_UNITS_HI : INDIAN_UNITS_EN
50
-
51
- if country == :in
52
- to_words_indian(number, language: language)
53
- else
54
- to_words_generic(val, exp_hash, include_and, ones_array, tens_array)
55
- end
53
+ to_words_generic(val, exp_hash, include_and, ones_array, tens_array)
56
54
  end
57
55
 
58
- # Indian system (Hindi & English)
56
+ # Indian system (supports English & Hindi)
59
57
  def to_words_indian(num, language: :hi)
60
58
  num = num.to_i
61
- return HINDI_NUMBERS_1_TO_99[num] if num <= 99
59
+ return HINDI_NUMBERS_1_TO_99[num] if language == :hi && num <= 99
60
+ return ONES_EN[num] if language == :en && num <= 19
62
61
 
63
62
  words = []
64
63
  unit_index = 0
@@ -66,8 +65,9 @@ module NumWords
66
65
  loop do
67
66
  break if num.zero?
68
67
  num, remainder = unit_index.zero? ? num.divmod(1000) : num.divmod(100)
69
- part = convert_hindi_sub_thousand(remainder)
70
- words.unshift("#{part} #{INDIAN_UNITS_HI[unit_index]}".strip) if remainder.positive?
68
+ part = language == :hi ? convert_hindi_sub_thousand(remainder) : convert_english_sub_thousand(remainder)
69
+ unit_name = language == :hi ? INDIAN_UNITS_HI[unit_index] : INDIAN_UNITS_EN[unit_index]
70
+ words.unshift("#{part} #{unit_name}".strip) if remainder.positive?
71
71
  unit_index += 1
72
72
  end
73
73
 
@@ -78,7 +78,6 @@ module NumWords
78
78
 
79
79
  def convert_hindi_sub_thousand(num)
80
80
  return HINDI_NUMBERS_1_TO_99[num] if num <= 99
81
-
82
81
  words = []
83
82
  if num >= 100
84
83
  words << "#{HINDI_NUMBERS_1_TO_99[num / 100]} सौ"
@@ -88,7 +87,19 @@ module NumWords
88
87
  words.join(' ').strip
89
88
  end
90
89
 
91
- # Generic system (English or other)
90
+ def convert_english_sub_thousand(num)
91
+ return ONES_EN[num] if num < 20
92
+ words = []
93
+ if num >= 100
94
+ words << "#{ONES_EN[num / 100]} hundred"
95
+ num %= 100
96
+ end
97
+ words << TENS_EN[num / 10] if num >= 20
98
+ num %= 10 if num >= 20
99
+ words << ONES_EN[num] if num.positive?
100
+ words.join(' ').strip
101
+ end
102
+
92
103
  def to_words_generic(val, exp_hash, include_and, ones_array, tens_array)
93
104
  chunks = []
94
105
  while val.positive?
@@ -99,25 +110,11 @@ module NumWords
99
110
  result = []
100
111
  chunks.each_with_index do |chunk, index|
101
112
  next if chunk.zero?
102
- words = hundreds_to_words(chunk, include_and && index.zero?, ones_array, tens_array)
113
+ words = convert_english_sub_thousand(chunk)
114
+ words = "and #{words}" if include_and && index.zero? && chunk >= 100
103
115
  result.unshift("#{words} #{exp_hash[index * 3]}".strip)
104
116
  end
105
117
  result.join(' ').strip
106
118
  end
107
-
108
- def hundreds_to_words(val, include_and, ones_array, tens_array)
109
- return '' if val.zero?
110
-
111
- words = []
112
- if val >= 100
113
- words << "#{ones_array[val / 100]} #{ones_array == HINDI_NUMBERS_1_TO_99 ? 'सौ' : 'hundred'}"
114
- val %= 100
115
- words << 'and' if val.positive? && include_and && ones_array != HINDI_NUMBERS_1_TO_99
116
- end
117
-
118
- words << tens_array[val] if val > 0 && val <= 99 && ones_array == HINDI_NUMBERS_1_TO_99
119
- words << ones_array[val] if val > 0 && val < 20 && ones_array != HINDI_NUMBERS_1_TO_99
120
- words.join(' ').strip
121
- end
122
119
  end
123
120
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NumWords
4
- VERSION = "0.5.0"
4
+ VERSION = "0.6.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: num_words
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - mrmalvi
@@ -9,8 +9,8 @@ bindir: exe
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies: []
12
- description: NumWords gem converts integers and decimals to words. Supports US, EU,
13
- UK, FR, and Indian number systems with proper handling of decimals.
12
+ description: NumWords gem converts integers and decimals to words. Supports US and
13
+ Indian number systems with proper handling of decimals.
14
14
  email:
15
15
  - malviyak00@gmail.com
16
16
  executables: