nr2w 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/nr2w.rb +73 -0
  3. data/lib/utils.rb +42 -0
  4. metadata +45 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fefefabd28e8d9b47af3e6d5e5da7c338020a1e028c22e2bbfee826f78cc1bf3
4
+ data.tar.gz: cea9f42270d77d8040b6e5bc2fb1636c8e18a5844105612168ef9eeb100e1b42
5
+ SHA512:
6
+ metadata.gz: 8a56bb118fdb56060924a5970caf66851788b5d822b98448e4be4a26d4d75684302c21462443c0651ec4c28f08458b665612ad8958214e440d413c20d1b20546
7
+ data.tar.gz: 84371880f2138cee37b6120517b3e6b9b4f363aa3dc707ea5dce78a55a0f745994c758c393b39421fd8b12bec6c16ef5729c29dd6781aedd841cd7a6b305613e
@@ -0,0 +1,73 @@
1
+ require_relative './utils'
2
+ include Utils
3
+
4
+ module Nr2w
5
+ @str = ''
6
+
7
+ class << self
8
+ # Units and dozens
9
+ def unit_dozen_hundred(hundreds_digits)
10
+ dozens = hundreds_digits.slice(0, 2).reverse.join.to_i
11
+ @str.prepend dozens_str(dozens)
12
+ return unless hundreds_digits.length > 2 && hundreds_digits[2]&.positive?
13
+
14
+ @str.prepend hundreds_str(hundreds_digits[2], dozens.positive?)
15
+ end
16
+
17
+ def convert(value)
18
+ deal_with_errors(value)
19
+
20
+ # Transform into positive and set negative as true
21
+ @negative =
22
+ if value.negative?
23
+ value = value.abs
24
+ true
25
+ end
26
+
27
+ (0...value.digits.count).step(3) do |idx|
28
+ hundreds_digits = value.digits.slice(idx, 3)
29
+ number_notation(idx) if hundreds_digits.reverse.join.to_i.positive?
30
+ unit_dozen_hundred(hundreds_digits)
31
+ end
32
+
33
+ @str.prepend('minus ') if @negative
34
+ @str = 'zero' if value.zero?
35
+
36
+ puts assemble_str(@str)
37
+ end
38
+
39
+ def deal_with_errors(value)
40
+ raise 'Not an integer' unless value.is_a? Integer
41
+ raise 'Overflow' unless value.abs < 2**100
42
+ end
43
+
44
+ def number_notation(index)
45
+ name =
46
+ case index
47
+ when 3
48
+ ' thousand '
49
+ when 6
50
+ ' million '
51
+ when 9
52
+ ' billion '
53
+ when 12
54
+ ' trillion '
55
+ when 15
56
+ ' quadrillion '
57
+ when 18
58
+ ' quintillion '
59
+ when 21
60
+ ' sextillion '
61
+ when 24
62
+ ' septillion '
63
+ when 27
64
+ ' octillion '
65
+ when 30
66
+ ' nonillion '
67
+ else
68
+ ''
69
+ end
70
+ @str.prepend name
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,42 @@
1
+ module Utils
2
+ def unit_digit(value)
3
+ array = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight',
4
+ 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen',
5
+ 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']
6
+ array[value]
7
+ end
8
+
9
+ def dozen_digit(value)
10
+ array = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy',
11
+ 'eigthy', 'ninety']
12
+ array[value]
13
+ end
14
+
15
+ def dozens_str(value)
16
+ # This validation is necessary because of the special name cases between 11 and 19, or else it would be value < 10
17
+ if value < 20 # EX: onze, doze, treze etc
18
+ unit_digit(value)
19
+ else
20
+ digits = value.digits.reverse
21
+ append = " #{unit_digit(digits[1])}" if unit_digit(digits[1]) != '' # EX: vinte OU vinte um
22
+ "#{dozen_digit(digits[0])}#{append}"
23
+ end
24
+ end
25
+
26
+ def hundreds_str(value, surplus = false)
27
+ append = surplus == false ? ' hundred' : ' hundred and '
28
+ unit_digit(value) + append
29
+ end
30
+
31
+ def assemble_str(str)
32
+ array = str.split(' ')
33
+ final_arr = []
34
+ array.each_with_index do |item, index|
35
+ final_arr << " #{item}"
36
+ if %w[nonillion octillion septill ion sextillion quintillion quadrillion trillion billion million thousand].include?(item) && index != array.size - 1
37
+ final_arr << ','
38
+ end
39
+ end
40
+ final_arr.reject(&:empty?).join('').strip
41
+ end
42
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nr2w
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - arbexmb
8
+ - callmarx
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2021-01-31 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: nr2w lets you convert number into words
15
+ email: lucasarbexmb@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/nr2w.rb
21
+ - lib/utils.rb
22
+ homepage: https://rubygems.org/gems/nr2w
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubygems_version: 3.1.2
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: number 2 words
45
+ test_files: []