number_to_words 1.1
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.
- data/Manifest +6 -0
- data/README.rdoc +54 -0
- data/Rakefile +12 -0
- data/init.rb +1 -0
- data/lib/number_to_words.rb +126 -0
- data/number_to_words.gemspec +30 -0
- metadata +66 -0
data/Manifest
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
= Number To Words
|
2
|
+
|
3
|
+
Rails gem/plugin to describe a number in words.
|
4
|
+
|
5
|
+
== Install
|
6
|
+
|
7
|
+
Add the following line to your /config/environment.rb file:
|
8
|
+
|
9
|
+
config.gem "innetra-number_to_words",
|
10
|
+
:lib => "number_to_words",
|
11
|
+
:source => "http://gems.github.com"
|
12
|
+
|
13
|
+
And from the command line run:
|
14
|
+
|
15
|
+
rake gems:install
|
16
|
+
|
17
|
+
===Install as a Plugin
|
18
|
+
|
19
|
+
If your rather prefer to install it as a plugin, from your application
|
20
|
+
directory simply run:
|
21
|
+
|
22
|
+
script/plugin install git://github.com/innetra/number_to_words.git
|
23
|
+
|
24
|
+
And you're done!
|
25
|
+
|
26
|
+
== Usage
|
27
|
+
|
28
|
+
For now it only works for spanish:
|
29
|
+
|
30
|
+
1235.to_words
|
31
|
+
=> "un mil doscientos treinta y cinco"
|
32
|
+
|
33
|
+
== License
|
34
|
+
|
35
|
+
Copyright (c) 2008 Innetra Consultancy Services, S.C.
|
36
|
+
|
37
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
38
|
+
a copy of this software and associated documentation files (the
|
39
|
+
"Software"), to deal in the Software without restriction, including
|
40
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
41
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
42
|
+
permit persons to whom the Software is furnished to do so, subject to
|
43
|
+
the following conditions:
|
44
|
+
|
45
|
+
The above copyright notice and this permission notice shall be
|
46
|
+
included in all copies or substantial portions of the Software.
|
47
|
+
|
48
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
49
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
50
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
51
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
52
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
53
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
54
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('number_to_words', '1.1') do |e|
|
6
|
+
e.description = "Gem for Ruby on Rails to describe a number in words"
|
7
|
+
e.url = "http://github.com/mexpolk/number_to_words"
|
8
|
+
e.author = "Ivan Torres"
|
9
|
+
e.email = "mexpolk@gmail.com"
|
10
|
+
e.ignore_pattern = ["tmp/*", "script/*"]
|
11
|
+
e.development_dependencies = []
|
12
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'number_to_words'
|
@@ -0,0 +1,126 @@
|
|
1
|
+
module NumberToWords
|
2
|
+
module Numeric
|
3
|
+
def self.included(recipient)
|
4
|
+
recipient.extend(ClassMethods)
|
5
|
+
recipient.class_eval do
|
6
|
+
include InstanceMethods
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
end
|
12
|
+
|
13
|
+
module InstanceMethods
|
14
|
+
|
15
|
+
|
16
|
+
def to_words
|
17
|
+
|
18
|
+
words = Array.new
|
19
|
+
|
20
|
+
number = self.to_i
|
21
|
+
|
22
|
+
if number.to_i == 0
|
23
|
+
words << self.zero_string
|
24
|
+
else
|
25
|
+
|
26
|
+
number = number.to_s.rjust(33,'0')
|
27
|
+
groups = number.scan(/.{3}/).reverse
|
28
|
+
|
29
|
+
|
30
|
+
words << number_to_words(groups[0])
|
31
|
+
|
32
|
+
(1..10).each do |number|
|
33
|
+
if groups[number].to_i > 0
|
34
|
+
case number
|
35
|
+
when 1,3,5,7,9
|
36
|
+
words << "mil"
|
37
|
+
else
|
38
|
+
words << (groups[number].to_i > 1 ? "#{self.quantities[number]}ones" : "#{self.quantities[number]}ón")
|
39
|
+
end
|
40
|
+
words << number_to_words(groups[number])
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
return "#{words.reverse.join(' ')}"
|
47
|
+
end
|
48
|
+
|
49
|
+
protected
|
50
|
+
|
51
|
+
def and_string
|
52
|
+
"y"
|
53
|
+
end
|
54
|
+
|
55
|
+
def zero_string
|
56
|
+
"cero"
|
57
|
+
end
|
58
|
+
|
59
|
+
def units
|
60
|
+
%w[ ~ un dos tres cuatro cinco seis siete ocho nueve ]
|
61
|
+
end
|
62
|
+
|
63
|
+
def tens
|
64
|
+
%w[ ~ diez veinte treinta cuarenta cincuenta sesenta setenta ochenta noventa ]
|
65
|
+
end
|
66
|
+
|
67
|
+
def hundreds
|
68
|
+
%w[ cien ciento doscientos trescientos cuatrocientos quinientos seiscientos setecientos ochocientos novecientos ]
|
69
|
+
end
|
70
|
+
|
71
|
+
def teens
|
72
|
+
%w[ diez once doce trece cartoce quince dieciseis diecisiete dieciocho diecinueve ]
|
73
|
+
end
|
74
|
+
|
75
|
+
def twenties
|
76
|
+
%w[ veinte veintiun veintidos veintitres veinticuatro veinticinco veintiseis veintisiete veintiocho veintinueve ]
|
77
|
+
end
|
78
|
+
|
79
|
+
def quantities
|
80
|
+
%w[ ~ ~ mill ~ bill ~ trill ~ cuatrill ~ quintill ~ ]
|
81
|
+
end
|
82
|
+
|
83
|
+
def number_to_words(number)
|
84
|
+
|
85
|
+
hundreds = number[0,1].to_i
|
86
|
+
tens = number[1,1].to_i
|
87
|
+
units = number[2,1].to_i
|
88
|
+
|
89
|
+
text = Array.new
|
90
|
+
|
91
|
+
if hundreds > 0
|
92
|
+
if hundreds == 1 && (tens + units == 0)
|
93
|
+
text << self.hundreds[0]
|
94
|
+
else
|
95
|
+
text << self.hundreds[hundreds]
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
if tens > 0
|
100
|
+
case tens
|
101
|
+
when 1
|
102
|
+
text << (units == 0 ? self.tens[tens] : self.teens[units])
|
103
|
+
when 2
|
104
|
+
text << (units == 0 ? self.tens[tens] : self.twenties[units])
|
105
|
+
else
|
106
|
+
text << self.tens[tens]
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
if units > 0
|
111
|
+
if tens == 0
|
112
|
+
text << self.units[units]
|
113
|
+
elsif tens > 2
|
114
|
+
text << "#{self.and_string} #{self.units[units]}"
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
return text.join(' ')
|
119
|
+
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
Numeric.send :include, NumberToWords::Numeric
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{number_to_words}
|
5
|
+
s.version = "1.1"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Ivan Torres"]
|
9
|
+
s.date = %q{2009-08-06}
|
10
|
+
s.description = %q{Gem for Ruby on Rails to describe a number in words}
|
11
|
+
s.email = %q{mexpolk@gmail.com}
|
12
|
+
s.extra_rdoc_files = ["README.rdoc", "lib/number_to_words.rb"]
|
13
|
+
s.files = ["number_to_words.gemspec", "Manifest", "README.rdoc", "Rakefile", "init.rb", "lib/number_to_words.rb"]
|
14
|
+
s.homepage = %q{http://github.com/mexpolk/number_to_words}
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Number_to_words", "--main", "README.rdoc"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = %q{number_to_words}
|
18
|
+
s.rubygems_version = %q{1.3.5}
|
19
|
+
s.summary = %q{Gem for Ruby on Rails to describe a number in words}
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
23
|
+
s.specification_version = 3
|
24
|
+
|
25
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
26
|
+
else
|
27
|
+
end
|
28
|
+
else
|
29
|
+
end
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: number_to_words
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "1.1"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ivan Torres
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-08-06 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Gem for Ruby on Rails to describe a number in words
|
17
|
+
email: mexpolk@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
- lib/number_to_words.rb
|
25
|
+
files:
|
26
|
+
- number_to_words.gemspec
|
27
|
+
- Manifest
|
28
|
+
- README.rdoc
|
29
|
+
- Rakefile
|
30
|
+
- init.rb
|
31
|
+
- lib/number_to_words.rb
|
32
|
+
has_rdoc: true
|
33
|
+
homepage: http://github.com/mexpolk/number_to_words
|
34
|
+
licenses: []
|
35
|
+
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options:
|
38
|
+
- --line-numbers
|
39
|
+
- --inline-source
|
40
|
+
- --title
|
41
|
+
- Number_to_words
|
42
|
+
- --main
|
43
|
+
- README.rdoc
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "1.2"
|
57
|
+
version:
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
rubyforge_project: number_to_words
|
61
|
+
rubygems_version: 1.3.5
|
62
|
+
signing_key:
|
63
|
+
specification_version: 3
|
64
|
+
summary: Gem for Ruby on Rails to describe a number in words
|
65
|
+
test_files: []
|
66
|
+
|