human_numbers 0.0.2
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 +7 -0
- data/lib/human_numbers.rb +4 -0
- data/lib/human_numbers/english.rb +121 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: eab6f2a8480be4a54bca1dbe85e24981d8f0c973
|
4
|
+
data.tar.gz: 44a2cb4efd2050c825efe632a0a472944fd2d719
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e0a289616134a215a91fae94a517c3fe0e81a4744bfc7e245ca86346e8c3ec9b92d068cf423b34467c3ae1aed399ff018d2c1be4e4f31ecdd2a4f422979f2171
|
7
|
+
data.tar.gz: d5c9d8b3d6d76a2e4bbe8e87bf7bd8ba2520f933efcb3c8739e2a7d1198c2dc63abdc8e52b47b89bcc66e8721e1b03a009ca043e432f7bc3b8ba42ab7ffb975d
|
@@ -0,0 +1,121 @@
|
|
1
|
+
module HumanNumbers::English
|
2
|
+
def self.cardinal_factor(n, name, factor)
|
3
|
+
cardinal_number(n / factor) + " #{name}" + cardinal_number(n % factor, ' ')
|
4
|
+
end
|
5
|
+
|
6
|
+
def self.cardinal_number(n, prefix='')
|
7
|
+
return prefix.empty? ? 'zero' : '' if n.zero?
|
8
|
+
if n >= 1_000_000_000_000_000_000_000_000_000_000_000
|
9
|
+
raise ArgumentError, 'number too large to print in English: #{n}'
|
10
|
+
end
|
11
|
+
|
12
|
+
prefix + if n >= 1_000_000_000_000_000_000_000_000_000_000
|
13
|
+
cardinal_factor(n, 'nonillion', 1_000_000_000_000_000_000_000_000_000_000)
|
14
|
+
elsif n >= 1_000_000_000_000_000_000_000_000_000
|
15
|
+
cardinal_factor(n, 'octillion', 1_000_000_000_000_000_000_000_000_000)
|
16
|
+
elsif n >= 1_000_000_000_000_000_000_000_000
|
17
|
+
cardinal_factor(n, 'septillion', 1_000_000_000_000_000_000_000_000)
|
18
|
+
elsif n >= 1_000_000_000_000_000_000_000
|
19
|
+
cardinal_factor(n, 'sextillion', 1_000_000_000_000_000_000_000)
|
20
|
+
elsif n >= 1_000_000_000_000_000_000
|
21
|
+
cardinal_factor(n, 'quintillion', 1_000_000_000_000_000_000)
|
22
|
+
elsif n >= 1_000_000_000_000_000
|
23
|
+
cardinal_factor(n, 'quadrillion', 1_000_000_000_000_000)
|
24
|
+
elsif n >= 1_000_000_000_000
|
25
|
+
cardinal_factor(n, 'trillion', 1_000_000_000_000)
|
26
|
+
elsif n >= 1_000_000_000
|
27
|
+
cardinal_factor(n, 'billion', 1_000_000_000)
|
28
|
+
elsif n >= 1_000_000
|
29
|
+
cardinal_factor(n, 'million', 1_000_000)
|
30
|
+
elsif n >= 1000
|
31
|
+
cardinal_factor(n, 'thousand', 1000)
|
32
|
+
elsif n >= 100
|
33
|
+
cardinal_factor(n, 'hundred', 100)
|
34
|
+
elsif n >= 20
|
35
|
+
simple_cardinal(n / 10 * 10) + simple_cardinal(n % 10, '-')
|
36
|
+
else
|
37
|
+
simple_cardinal(n)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.simple_cardinal(n, prefix='')
|
42
|
+
return '' if n.zero?
|
43
|
+
prefix + case n
|
44
|
+
when 1; 'one'
|
45
|
+
when 2; 'two'
|
46
|
+
when 3; 'three'
|
47
|
+
when 4; 'four'
|
48
|
+
when 5; 'five'
|
49
|
+
when 6; 'six'
|
50
|
+
when 7; 'seven'
|
51
|
+
when 8; 'eight'
|
52
|
+
when 9; 'nine'
|
53
|
+
when 10; 'ten'
|
54
|
+
when 11; 'eleven'
|
55
|
+
when 12; 'twelve'
|
56
|
+
when 13; 'thirteen'
|
57
|
+
when 14; 'fourteen'
|
58
|
+
when 15; 'fifteen'
|
59
|
+
when 16; 'sixteen'
|
60
|
+
when 17; 'seventeen'
|
61
|
+
when 18; 'eighteen'
|
62
|
+
when 19; 'nineteen'
|
63
|
+
when 20; 'twenty'
|
64
|
+
when 30; 'thirty'
|
65
|
+
when 40; 'forty'
|
66
|
+
when 50; 'fifty'
|
67
|
+
when 60; 'sixty'
|
68
|
+
when 70; 'seventy'
|
69
|
+
when 80; 'eighty'
|
70
|
+
when 90; 'ninety'
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.ordinal_number(n)
|
75
|
+
case n
|
76
|
+
when 1; 'first'
|
77
|
+
when 2; 'second'
|
78
|
+
when 3; 'third'
|
79
|
+
when 4; 'fourth'
|
80
|
+
when 5; 'fifth'
|
81
|
+
when 8; 'eighth'
|
82
|
+
when 9; 'ninth'
|
83
|
+
when 12; 'twelfth'
|
84
|
+
else
|
85
|
+
if n > 10 && n < 100 && n % 10 == 0
|
86
|
+
simple_cardinal(n)[0..-2] + 'ieth'
|
87
|
+
elsif n >= 20 && n < 100
|
88
|
+
simple_cardinal(n / 10 * 10) + '-' + ordinal_number(n % 10)
|
89
|
+
elsif n > 100
|
90
|
+
rem = n % 100
|
91
|
+
cardinal_number(n / 100 * 100) +
|
92
|
+
(rem.zero? ? 'th' : ' ' + ordinal_number(rem))
|
93
|
+
else
|
94
|
+
cardinal_number(n) + 'th'
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
class Integer
|
101
|
+
def to_english(style = :cardinal)
|
102
|
+
string = (self < 0 ? 'negative ' : '') +
|
103
|
+
case style
|
104
|
+
when :ordinal; HumanNumbers::English::ordinal_number(self.abs)
|
105
|
+
when :cardinal; HumanNumbers::English::cardinal_number(self.abs)
|
106
|
+
else raise ArgumentError, 'unrecognized number style: #{style}'
|
107
|
+
end
|
108
|
+
if self > 100 and self % 100 != 0
|
109
|
+
string.gsub(/(.*) /, '\1 and ')
|
110
|
+
else
|
111
|
+
string
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
class Float
|
117
|
+
def to_english(style = :cardinal)
|
118
|
+
"#{self.to_i.to_english(:cardinal)} point " +
|
119
|
+
"#{self.to_s.sub(/.*\./, '').to_i.to_english(style)}"
|
120
|
+
end
|
121
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: human_numbers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kyle Brown
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: simplecov
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.12.0
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.12.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest-reporters
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.1.11
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: 1.1.11
|
37
|
+
type: :development
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - "~>"
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.1.11
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 1.1.11
|
47
|
+
description: |
|
48
|
+
human_numbers defines the methods Float#to_english and Integer#to_english for
|
49
|
+
converting numbers to English strings. By default, a cardinal number will be
|
50
|
+
returned (one, two, three), but supplying an :ordinal argument will return an
|
51
|
+
ordinal (first, second, third). Handles numbers with absolute value less than
|
52
|
+
1000000000000000000000000000000000.
|
53
|
+
email: picokyle@gmail.com
|
54
|
+
executables: []
|
55
|
+
extensions: []
|
56
|
+
extra_rdoc_files: []
|
57
|
+
files:
|
58
|
+
- lib/human_numbers.rb
|
59
|
+
- lib/human_numbers/english.rb
|
60
|
+
homepage: https://github.com/kybp/human_numbers
|
61
|
+
licenses:
|
62
|
+
- MIT
|
63
|
+
metadata: {}
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 2.5.1
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: Print numbers in English
|
84
|
+
test_files: []
|