num_words 0.5.0 → 0.7.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: 0704aa1beffbb9b928b9e2430f73dddffb0de283d0d008a09ab385e1df828d92
4
+ data.tar.gz: 8298622791967a587e816e38b9b15df7f916d50d59e18609d90106b3b98ea650
5
5
  SHA512:
6
- metadata.gz: 75c3b9d42dc93e10d5a413d2f1a03b04921f8277dd1ae056bcf4f020c9672e6e4737752d82d02541e9ac9b0560e0facb5888e64cb3de72ee83dcb881855f27b0
7
- data.tar.gz: 666c1624f2d5c82ac29cd479f3c1625f0f43941be7876d190b3483ff87de195d4c949b9d88b74b9c10e8886f64e2657d91812ce85a2041fd5705d4038900208c
6
+ metadata.gz: 678c4767f6bbbbe28bc8b4b30785ef6c439971769014c595da1307f7f33ffc986bc86ea3cecbeae257d1620c854db4ae6dcb2b60d516a927a3ab4664ee909d5e
7
+ data.tar.gz: af84cb1f4809f7c2d3b2a214801771067b2f8191aa815d7477d26ef7a75099213fe50189fc734aef01c92cd688bddcf2d86530937773e4fe294645deacb65673
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,
@@ -34,31 +37,36 @@ module NumWords
34
37
  }.freeze
35
38
 
36
39
  class << self
37
- # Convert number to words
40
+ # Convert number to words (supports decimals)
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
- val = number.to_i.abs
44
- return 'zero' if val.zero?
45
-
46
- exp_hash = COUNTRY_EXPONENTS[country] || AM_EXPONENTS
47
- ones_array = language == :hi ? HINDI_NUMBERS_1_TO_99 : ONES_EN
48
- 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
43
+ return language == :hi ? HINDI_NUMBERS_1_TO_99[0] : 'zero' if number.to_f.zero?
44
+
45
+ integer_part, decimal_part = number.to_s.split('.')
46
+ integer_words = if country == :in
47
+ language == :hi ? to_words_indian(integer_part.to_i) : to_words_indian(integer_part.to_i, language: :en)
48
+ else
49
+ exp_hash = COUNTRY_EXPONENTS[country] || AM_EXPONENTS
50
+ ones_array = language == :hi ? HINDI_NUMBERS_1_TO_99 : ONES_EN
51
+ tens_array = language == :hi ? HINDI_NUMBERS_1_TO_99 : TENS_EN
52
+ to_words_generic(integer_part.to_i, exp_hash, include_and, ones_array, tens_array)
53
+ end
54
+
55
+ return integer_words if decimal_part.nil? || decimal_part.to_i.zero?
56
+
57
+ decimal_words = decimal_part.chars.map do |d|
58
+ language == :hi ? HINDI_NUMBERS_1_TO_99[d.to_i] : ONES_EN[d.to_i]
59
+ end.join(' ')
60
+
61
+ connector = language == :hi ? 'अंक' : 'point'
62
+ "#{integer_words} #{connector} #{decimal_words}"
56
63
  end
57
64
 
58
- # Indian system (Hindi & English)
65
+ # Indian system (supports English & Hindi)
59
66
  def to_words_indian(num, language: :hi)
60
67
  num = num.to_i
61
- return HINDI_NUMBERS_1_TO_99[num] if num <= 99
68
+ return HINDI_NUMBERS_1_TO_99[num] if language == :hi && num <= 99
69
+ return ONES_EN[num] if language == :en && num <= 19
62
70
 
63
71
  words = []
64
72
  unit_index = 0
@@ -66,8 +74,9 @@ module NumWords
66
74
  loop do
67
75
  break if num.zero?
68
76
  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?
77
+ part = language == :hi ? convert_hindi_sub_thousand(remainder) : convert_english_sub_thousand(remainder)
78
+ unit_name = language == :hi ? INDIAN_UNITS_HI[unit_index] : INDIAN_UNITS_EN[unit_index]
79
+ words.unshift("#{part} #{unit_name}".strip) if remainder.positive?
71
80
  unit_index += 1
72
81
  end
73
82
 
@@ -78,7 +87,6 @@ module NumWords
78
87
 
79
88
  def convert_hindi_sub_thousand(num)
80
89
  return HINDI_NUMBERS_1_TO_99[num] if num <= 99
81
-
82
90
  words = []
83
91
  if num >= 100
84
92
  words << "#{HINDI_NUMBERS_1_TO_99[num / 100]} सौ"
@@ -88,7 +96,19 @@ module NumWords
88
96
  words.join(' ').strip
89
97
  end
90
98
 
91
- # Generic system (English or other)
99
+ def convert_english_sub_thousand(num)
100
+ return ONES_EN[num] if num < 20
101
+ words = []
102
+ if num >= 100
103
+ words << "#{ONES_EN[num / 100]} hundred"
104
+ num %= 100
105
+ end
106
+ words << TENS_EN[num / 10] if num >= 20
107
+ num %= 10 if num >= 20
108
+ words << ONES_EN[num] if num.positive?
109
+ words.join(' ').strip
110
+ end
111
+
92
112
  def to_words_generic(val, exp_hash, include_and, ones_array, tens_array)
93
113
  chunks = []
94
114
  while val.positive?
@@ -99,25 +119,11 @@ module NumWords
99
119
  result = []
100
120
  chunks.each_with_index do |chunk, index|
101
121
  next if chunk.zero?
102
- words = hundreds_to_words(chunk, include_and && index.zero?, ones_array, tens_array)
122
+ words = convert_english_sub_thousand(chunk)
123
+ words = "and #{words}" if include_and && index.zero? && chunk >= 100
103
124
  result.unshift("#{words} #{exp_hash[index * 3]}".strip)
104
125
  end
105
126
  result.join(' ').strip
106
127
  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
128
  end
123
129
  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.7.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.7.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: