sankhya 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1d2e0c7d806546a25964eb799e9ac93539551e28
4
+ data.tar.gz: ae607af6d1fd20ab6502d7d2b5dbb1290810b3cd
5
+ SHA512:
6
+ metadata.gz: 29322362d617f075f04b66026f628b0ae277d305a7ec7dc4cebb231781cb061cab0f17e13169e92c17e730832c8183d9c299eedfab5a5b62b76635f914b71cd7
7
+ data.tar.gz: 08506902eeebf36dea735e625e4461df50649f8df502f5cc3539e52632a84ba0084ef50f9432a3d09ae6393be8dd0b91602be5ad0a09ea535b8485cb5ec7fedd
@@ -0,0 +1,2 @@
1
+ .bundle/*
2
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sankhya.gemspec
4
+ gemspec
@@ -0,0 +1,27 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ sankhya (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.2.5)
10
+ rake (10.1.1)
11
+ rspec (2.14.1)
12
+ rspec-core (~> 2.14.0)
13
+ rspec-expectations (~> 2.14.0)
14
+ rspec-mocks (~> 2.14.0)
15
+ rspec-core (2.14.7)
16
+ rspec-expectations (2.14.5)
17
+ diff-lcs (>= 1.1.3, < 2.0)
18
+ rspec-mocks (2.14.5)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ bundler (~> 1.5)
25
+ rake
26
+ rspec
27
+ sankhya!
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Geordee Naliyath
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,39 @@
1
+ # Sankhya
2
+
3
+ Sankhya is a little gem to convert number to words in Indian Numbering System.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'sankhya'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install sankhya
18
+
19
+ ## Usage
20
+
21
+ Sankhya add a 'to_words' method to Integer and Float classes.
22
+
23
+ 10101101.to_words # "one crore, one lakh, one thousand, one hundred and one"
24
+ 0.to_words # "zero"
25
+
26
+ 1.1.to_words # ["one", "ten"]
27
+ 1.1.to_words # ["one", "one"]
28
+ 1.01.to_words # ["one", "one"]
29
+
30
+
31
+ Please see the spec for more examples.
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( http://github.com/samyukti/sankhya/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,12 @@
1
+ require "sankhya/version"
2
+ require "sankhya/words"
3
+ require "sankhya/numbers"
4
+
5
+ module Sankhya
6
+ def to_words(options = {})
7
+ Numbers::translate self, options[:scale]
8
+ end
9
+ end
10
+
11
+ Integer.send :include, Sankhya
12
+ Float.send :include, Sankhya
@@ -0,0 +1,57 @@
1
+ module Sankhya
2
+
3
+ class Numbers
4
+ extend Words
5
+
6
+ def self.translate(number, scale)
7
+ scale ||= 2
8
+
9
+ if number.integer?
10
+ # integer
11
+ words_of(number)
12
+ else
13
+ # float
14
+ numbers = number.to_s.split('.')
15
+ [words_of(numbers.first.to_i), words_of(numbers.last.ljust(scale,'0')[0..scale].to_i)]
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def self.words_of(number)
22
+ if number < 20
23
+ self.to_english(number)
24
+ else
25
+ words = []
26
+
27
+ units = [{divisor: 100, prefix: ', and' },
28
+ {divisor: 10, suffix: 'hundred,' },
29
+ {divisor: 100, suffix: 'thousand,' },
30
+ {divisor: 100, suffix: 'lakh,' }]
31
+
32
+ i = 0
33
+ while i < units.length
34
+ number, segment = number.divmod(units[i][:divisor])
35
+ parts = segment < 20 ? segment.divmod(100) : segment.divmod(10)
36
+
37
+ words << units[i][:suffix] if units[i][:suffix] and segment > 0
38
+ words << self.to_english(parts.last) if parts.last > 0
39
+ words << self.to_english(10 * parts.first) if parts.first > 0
40
+ words << units[i][:prefix] if units[i][:prefix] and segment > 0 and number > 0
41
+
42
+ if i == units.length - 1 and number > 0
43
+ # suffix crore and and start over
44
+ words << 'crore,'
45
+ i = 0
46
+ else
47
+ i += 1
48
+ end
49
+ end
50
+
51
+ words.reverse.join(' ').gsub(/, ,|,$/, '')
52
+ end
53
+ end
54
+
55
+ end
56
+
57
+ end
@@ -0,0 +1,3 @@
1
+ module Sankhya
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,40 @@
1
+ module Sankhya
2
+ module Words
3
+ def to_english(number)
4
+ {0 => 'zero',
5
+ 1 => 'one',
6
+ 2 => 'two',
7
+ 3 => 'three',
8
+ 4 => 'four',
9
+ 5 => 'five',
10
+ 6 => 'six',
11
+ 7 => 'seven',
12
+ 8 => 'eight',
13
+ 9 => 'nine',
14
+ 10 => 'ten',
15
+ 11 => 'eleven',
16
+ 12 => 'twelve',
17
+ 13 => 'thirteen',
18
+ 14 => 'fourteen',
19
+ 15 => 'fifteen',
20
+ 16 => 'sixteen',
21
+ 17 => 'seventeen',
22
+ 18 => 'eighteen',
23
+ 19 => 'nineteen',
24
+ 10 => 'ten',
25
+ 20 => 'twenty',
26
+ 30 => 'thirty',
27
+ 40 => 'forty',
28
+ 50 => 'fifty',
29
+ 60 => 'sixty',
30
+ 70 => 'seventy',
31
+ 80 => 'eighty',
32
+ 90 => 'ninety',
33
+ 100 => 'hundred',
34
+ 1000 => 'thousand',
35
+ 100000 => 'lakh',
36
+ 10000000 => 'crore'
37
+ }[number]
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sankhya/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sankhya"
8
+ spec.version = Sankhya::VERSION
9
+ spec.authors = ["Geordee Naliyath"]
10
+ spec.email = ["geordee@gmail.com"]
11
+ spec.summary = %q{A little gem to convert number to words in Indian English}
12
+ spec.description = %q{Sankhya means numbers in Sanskrit and other Indian languages. This is a small gem to convert numbers to words in Indian English.}
13
+ spec.homepage = "https://github.com/samyukti/sankhya"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sankhya do
4
+
5
+ it "converts 0 to words" do
6
+ 0.to_words.should eql("zero")
7
+ end
8
+
9
+ it "converts 5,36,24,133 to words" do
10
+ 53624133.to_words.should eql("five crore, thirty six lakh, twenty four thousand, one hundred and thirty three")
11
+ end
12
+
13
+ it "converts 12,12,012 to words" do
14
+ 1212012.to_words.should eql("twelve lakh, twelve thousand and twelve")
15
+ end
16
+
17
+ it "converts 7,00,007 to words" do
18
+ 700007.to_words.should eql("seven lakh and seven")
19
+ end
20
+
21
+ it "converts 1,01,01,101 to words" do
22
+ 10101101.to_words.should eql("one crore, one lakh, one thousand, one hundred and one")
23
+ end
24
+
25
+ it "converts 33,00,00,000 to words" do
26
+ 330000000.to_words.should eql("thirty three crore")
27
+ end
28
+
29
+ it "converts 10,00,000 to words" do
30
+ 1000000.to_words.should eql("ten lakh")
31
+ end
32
+
33
+ it "converts 99,000 to words" do
34
+ 99000.to_words.should eql("ninety nine thousand")
35
+ end
36
+
37
+ it "converts 0.0 to words" do
38
+ (0.0).to_words.should eql(["zero", "zero"])
39
+ end
40
+
41
+ it "converts 1.1 to words" do
42
+ (1.1).to_words.should eql(["one", "ten"])
43
+ end
44
+
45
+ it "converts 1.1 with scale 1 to words" do
46
+ (1.1).to_words(scale: 1).should eql(["one", "one"])
47
+ end
48
+
49
+ it "converts 1.01 with scale 2 to words" do
50
+ (1.01).to_words(scale: 2).should eql(["one", "one"])
51
+ end
52
+
53
+ it "converts 3.14 to words" do
54
+ (3.14).to_words.should eql(["three", "fourteen"])
55
+ end
56
+
57
+ it "converts 29,97,924.58 to words" do
58
+ (2997924.58).to_words.should eql(["twenty nine lakh, ninety seven thousand, nine hundred and twenty four", "fifty eight"])
59
+ end
60
+
61
+ it "converts 143,29,97,924.58 to words" do
62
+ (1432997924.58).to_words.should eql(["one hundred and forty three crore, twenty nine lakh, ninety seven thousand, nine hundred and twenty four", "fifty eight"])
63
+ end
64
+
65
+ it "converts 2,07,143,29,97,924.58 to words" do
66
+ (2071432997924.58).to_words.should eql(["two lakh, seven thousand, one hundred and forty three crore, twenty nine lakh, ninety seven thousand, nine hundred and twenty four", "fifty eight"])
67
+ end
68
+
69
+ end
@@ -0,0 +1,8 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'sankhya'
5
+
6
+ RSpec.configure do |config|
7
+
8
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sankhya
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Geordee Naliyath
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Sankhya means numbers in Sanskrit and other Indian languages. This is
56
+ a small gem to convert numbers to words in Indian English.
57
+ email:
58
+ - geordee@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .gitignore
64
+ - Gemfile
65
+ - Gemfile.lock
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/sankhya.rb
70
+ - lib/sankhya/numbers.rb
71
+ - lib/sankhya/version.rb
72
+ - lib/sankhya/words.rb
73
+ - sankhya.gemspec
74
+ - spec/sankhya_spec.rb
75
+ - spec/spec_helper.rb
76
+ homepage: https://github.com/samyukti/sankhya
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.0.6
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: A little gem to convert number to words in Indian English
100
+ test_files:
101
+ - spec/sankhya_spec.rb
102
+ - spec/spec_helper.rb