Skip to content
Browse files

initial commit

  • Loading branch information...
0 parents commit f2d3456cf86484a60907607b89d576540a0c6c96 Ivan Torres committed Aug 6, 2009
Showing with 200 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +6 −0 Manifest
  3. +54 −0 README.rdoc
  4. +12 −0 Rakefile
  5. +1 −0 init.rb
  6. +126 −0 lib/number_to_words.rb
1 .gitignore
@@ -0,0 +1 @@
+pkg/*
6 Manifest
@@ -0,0 +1,6 @@
+number_to_words.gemspec
+Manifest
+README.rdoc
+Rakefile
+init.rb
+lib/number_to_words.rb
54 README.rdoc
@@ -0,0 +1,54 @@
+= Number To Words
+
+Rails gem/plugin to describe a number in words.
+
+== Install
+
+Add the following line to your /config/environment.rb file:
+
+ config.gem "innetra-number_to_words",
+ :lib => "number_to_words",
+ :source => "http://gems.github.com"
+
+And from the command line run:
+
+ rake gems:install
+
+===Install as a Plugin
+
+If your rather prefer to install it as a plugin, from your application
+directory simply run:
+
+ script/plugin install git://github.com/innetra/number_to_words.git
+
+And you're done!
+
+== Usage
+
+For now it only works for spanish:
+
+ 1235.to_words
+ => "un mil doscientos treinta y cinco"
+
+== License
+
+Copyright (c) 2008 Innetra Consultancy Services, S.C.
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 Rakefile
@@ -0,0 +1,12 @@
+require 'rubygems'
+require 'rake'
+require 'echoe'
+
+Echoe.new('number_to_words', '1.0') do |e|
+ e.description = "Gem for Ruby on Rails to describe a number in words"
+ e.url = "http://github.com/mexpolk/number_to_words"
+ e.author = "Ivan Torres"
+ e.email = "mexpolk@gmail.com"
+ e.ignore_pattern = ["tmp/*", "script/*"]
+ e.development_dependencies = []
+end
1 init.rb
@@ -0,0 +1 @@
+require 'number_to_words'
126 lib/number_to_words.rb
@@ -0,0 +1,126 @@
+module NumberToWords
+ module Numeric
+ def self.included(recipient)
+ recipient.extend(ClassMethods)
+ recipient.class_eval do
+ include InstanceMethods
+ end
+ end
+
+ module ClassMethods
+ end
+
+ module InstanceMethods
+
+
+ def to_words
+
+ words = Array.new
+
+ number = self.to_i
+
+ if number.to_i == 0
+ words << self.zero_string
+ else
+
+ number = number.to_s.rjust(33,'0')
+ groups = number.scan(/.{3}/).reverse
+
+
+ words << number_to_words(groups[0])
+
+ (1..10).each do |number|
+ if groups[number].to_i > 0
+ case number
+ when 1,3,5,7,9
+ words << "mil"
+ else
+ words << (groups[number].to_i > 1 ? "#{self.quantities[number]}ones" : "#{self.quantities[number]}ón")
+ end
+ words << number_to_words(groups[number])
+ end
+ end
+
+ end
+
+ return "#{words.reverse.join(' ')}"
+ end
+
+ protected
+
+ def and_string
+ "y"
+ end
+
+ def zero_string
+ "cero"
+ end
+
+ def units
+ %w[ ~ un dos tres cuatro cinco seis siete ocho nueve ]
+ end
+
+ def tens
+ %w[ ~ diez veinte treinta cuarenta cincuenta sesenta setenta ochenta noventa ]
+ end
+
+ def hundreds
+ %w[ cien ciento doscientos trescientos cuatrocientos quinientos seiscientos setecientos ochocientos novecientos ]
+ end
+
+ def teens
+ %w[ diez once doce trece cartoce quince dieciseis diecisiete dieciocho diecinueve ]
+ end
+
+ def twenties
+ %w[ veinte veintiun veintidos veintitres veinticuatro veinticinco veintiseis veintisiete veintiocho veintinueve ]
+ end
+
+ def quantities
+ %w[ ~ ~ mill ~ bill ~ trill ~ cuatrill ~ quintill ~ ]
+ end
+
+ def number_to_words(number)
+
+ hundreds = number[0,1].to_i
+ tens = number[1,1].to_i
+ units = number[2,1].to_i
+
+ text = Array.new
+
+ if hundreds > 0
+ if hundreds == 1 && (tens + units == 0)
+ text << self.hundreds[0]
+ else
+ text << self.hundreds[hundreds]
+ end
+ end
+
+ if tens > 0
+ case tens
+ when 1
+ text << (units == 0 ? self.tens[tens] : self.teens[units])
+ when 2
+ text << (units == 0 ? self.tens[tens] : self.twenties[units])
+ else
+ text << self.tens[tens]
+ end
+ end
+
+ if units > 0
+ if tens == 0
+ text << self.units[units]
+ elsif tens > 2
+ text << "#{self.and_string} #{self.units[units]}"
+ end
+ end
+
+ return text.join(' ')
+
+ end
+
+ end
+ end
+end
+
+Numeric.send :include, NumberToWords::Numeric

0 comments on commit f2d3456

Please sign in to comment.
Something went wrong with that request. Please try again.