number_names 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +5 -0
- data/Rakefile +1 -0
- data/lib/number_names.rb +5 -0
- data/lib/number_names/core_ext.rb +5 -0
- data/lib/number_names/name_parser.rb +88 -0
- data/lib/number_names/version.rb +3 -0
- data/number_names.gemspec +23 -0
- data/spec/name_parser_spec.rb +37 -0
- metadata +72 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/number_names.rb
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
module NumberNames::NameParser
|
2
|
+
|
3
|
+
def to_name
|
4
|
+
num_str = self.to_s
|
5
|
+
while num_str.length % 3 != 0 do num_str = "0" + num_str end
|
6
|
+
n = num_str.scan /.../
|
7
|
+
|
8
|
+
n.each_with_index.map do |e, i|
|
9
|
+
name = ""
|
10
|
+
name << "and " if(i == n.size - 1 && n.size > 1 && e.to_i < 100 && e.to_i > 0)
|
11
|
+
name << get_number_name(e, n.size - i)
|
12
|
+
end.join(" ").strip
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
def get_number_name(num, place)
|
17
|
+
# determine hundreds place
|
18
|
+
hundreds = "#{NUMBER_NAMES[:digits][num[0]]} hundred" unless num[0] == "0"
|
19
|
+
hundreds += " and " if hundreds && num[1,2] != "00"
|
20
|
+
# get index for tens
|
21
|
+
if num[1,2].to_i < 20
|
22
|
+
tens = "#{NUMBER_NAMES[:digits][num[1,2].to_i.to_s]}"
|
23
|
+
else
|
24
|
+
tens = "#{NUMBER_NAMES[:tens][num[1]]} #{NUMBER_NAMES[:digits][num[2]]}"
|
25
|
+
end
|
26
|
+
|
27
|
+
hundreds.to_s + tens.to_s + NUMBER_NAMES[:position][place.to_s].to_s
|
28
|
+
end
|
29
|
+
|
30
|
+
NUMBER_NAMES = {
|
31
|
+
:tens => {
|
32
|
+
"0" => "",
|
33
|
+
"2" => "twenty",
|
34
|
+
"3" => "thirty",
|
35
|
+
"4" => "forty",
|
36
|
+
"5" => "fifty",
|
37
|
+
"6" => "sixty",
|
38
|
+
"7" => "seventy",
|
39
|
+
"8" => "eighty",
|
40
|
+
"9" => "ninety"
|
41
|
+
},
|
42
|
+
:digits => {
|
43
|
+
"1" => "one",
|
44
|
+
"2" => "two",
|
45
|
+
"3" => "three",
|
46
|
+
"4" => "four",
|
47
|
+
"5" => "five",
|
48
|
+
"6" => "six",
|
49
|
+
"7" => "seven",
|
50
|
+
"8" => "eight",
|
51
|
+
"9" => "nine",
|
52
|
+
"10" => "ten",
|
53
|
+
"11" => "eleven",
|
54
|
+
"12" => "twelve",
|
55
|
+
"13" => "thirteen",
|
56
|
+
"14" => "fourteen",
|
57
|
+
"15" => "fifteen",
|
58
|
+
"16" => "sixteen",
|
59
|
+
"17" => "seventeen",
|
60
|
+
"18" => "eighteen",
|
61
|
+
"19" => "nineteen"
|
62
|
+
},
|
63
|
+
:position => {
|
64
|
+
"2" => " thousand",
|
65
|
+
"3" => " million",
|
66
|
+
"4" => " billion",
|
67
|
+
"5" => " trillion",
|
68
|
+
"6" => " quadrillion",
|
69
|
+
"7" => " quintillion",
|
70
|
+
"8" => " sextillion",
|
71
|
+
"9" => " septillion",
|
72
|
+
"10" => " octillion",
|
73
|
+
"11" => " nonillion",
|
74
|
+
"12" => " decillion",
|
75
|
+
"13" => " undecillion",
|
76
|
+
"14" => " duodecillion",
|
77
|
+
"15" => " tredecillion",
|
78
|
+
"16" => " quattuordecillion",
|
79
|
+
"17" => " quindecillion",
|
80
|
+
"18" => " sexdecillion",
|
81
|
+
"19" => " septendecillion",
|
82
|
+
"20" => " octodecillion",
|
83
|
+
"21" => " novemdecillion",
|
84
|
+
"22" => " vigintillion"
|
85
|
+
}
|
86
|
+
}
|
87
|
+
|
88
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "number_names/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "number_names"
|
7
|
+
s.version = NumberNames::VERSION
|
8
|
+
s.authors = ["Tyler Dooling"]
|
9
|
+
s.email = ["wtdooling@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{A simple to covert numbers to their string names.}
|
12
|
+
s.description = %q{A simple to covert numbers to their string names.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "number_names"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# dependencies
|
22
|
+
s.add_development_dependency "rspec"
|
23
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'number_names/core_ext'
|
2
|
+
|
3
|
+
describe NumberNames::NameParser do
|
4
|
+
it "should parse single digit numbers" do
|
5
|
+
1.to_name.should == "one"
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should parse 2 digit numbers" do
|
9
|
+
10.to_name.should == "ten"
|
10
|
+
25.to_name.should == "twenty five"
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should parse 3 digit numbers" do
|
14
|
+
100.to_name.should == "one hundred"
|
15
|
+
256.to_name.should == "two hundred and fifty six"
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should parse 4 digit numbers" do
|
19
|
+
1000.to_name.should == "one thousand"
|
20
|
+
6256.to_name.should == "six thousand two hundred and fifty six"
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should parse 5 digit numbers" do
|
24
|
+
10000.to_name.should == "ten thousand"
|
25
|
+
76256.to_name.should == "seventy six thousand two hundred and fifty six"
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should parse 5 digit numbers" do
|
29
|
+
10000.to_name.should == "ten thousand"
|
30
|
+
76256.to_name.should == "seventy six thousand two hundred and fifty six"
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should parse big numbers" do
|
34
|
+
1073741823.to_name.should == "one billion seventy three million seven hundred and forty one thousand eight hundred and twenty three"
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: number_names
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tyler Dooling
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-21 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &2157953080 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2157953080
|
25
|
+
description: A simple to covert numbers to their string names.
|
26
|
+
email:
|
27
|
+
- wtdooling@gmail.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- Rakefile
|
35
|
+
- lib/number_names.rb
|
36
|
+
- lib/number_names/core_ext.rb
|
37
|
+
- lib/number_names/name_parser.rb
|
38
|
+
- lib/number_names/version.rb
|
39
|
+
- number_names.gemspec
|
40
|
+
- spec/name_parser_spec.rb
|
41
|
+
homepage: ''
|
42
|
+
licenses: []
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
hash: 1378411451069870196
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
hash: 1378411451069870196
|
65
|
+
requirements: []
|
66
|
+
rubyforge_project: number_names
|
67
|
+
rubygems_version: 1.8.10
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: A simple to covert numbers to their string names.
|
71
|
+
test_files:
|
72
|
+
- spec/name_parser_spec.rb
|