sankhya 0.1.0 → 0.2.0
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 +4 -4
- data/Gemfile.lock +1 -1
- data/lib/sankhya/numbers.rb +17 -8
- data/lib/sankhya/version.rb +1 -1
- data/lib/sankhya.rb +1 -1
- data/spec/sankhya_spec.rb +43 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 78a4f985011da8598c53c68f0d3b0811f717d50c
|
4
|
+
data.tar.gz: 553f1c68691e7e7a8413cf5ad1d2c42394ce8cf7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4b5db5eb2714e808c586fc155028717c2041e6044c32ce0400f1fcd1e8aed684f7fe2784fa69b4488c1ea98ac6c75fb568416c8d94a0e101f8fa9e13ebb1a01d
|
7
|
+
data.tar.gz: 6f29b000505cdf23733284b73d720fb28e3c05be137498d6cb281dca9234d2878d0b0b1f528234d558ba818c0a5eb11f8d56e208ed976bd681d719fcdf80a58d
|
data/Gemfile.lock
CHANGED
data/lib/sankhya/numbers.rb
CHANGED
@@ -3,21 +3,29 @@ module Sankhya
|
|
3
3
|
class Numbers
|
4
4
|
extend Words
|
5
5
|
|
6
|
-
def self.translate(number,
|
7
|
-
|
6
|
+
def self.translate(number, options)
|
7
|
+
parse options
|
8
8
|
|
9
9
|
if number.integer?
|
10
|
-
# integer
|
11
10
|
words_of(number)
|
12
|
-
else
|
13
|
-
|
14
|
-
|
15
|
-
[words_of(
|
11
|
+
else # if float
|
12
|
+
integer = number.floor
|
13
|
+
decimal = (number * (10 ** @scale)).floor - (integer * (10 ** @scale))
|
14
|
+
[words_of(integer), words_of(decimal)]
|
16
15
|
end
|
17
16
|
end
|
18
17
|
|
19
18
|
private
|
20
19
|
|
20
|
+
def self.is_number?(number)
|
21
|
+
true if Float(number) rescue false
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.parse(options)
|
25
|
+
@comma = options.has_key?(:comma) ? options[:comma] : true
|
26
|
+
@scale = is_number?(options[:scale]) ? ([options[:scale], 0].max) : 2
|
27
|
+
end
|
28
|
+
|
21
29
|
def self.words_of(number)
|
22
30
|
if number < 20
|
23
31
|
self.to_english(number)
|
@@ -48,7 +56,8 @@ module Sankhya
|
|
48
56
|
end
|
49
57
|
end
|
50
58
|
|
51
|
-
|
59
|
+
regex = @comma ? /, ,|,$/ : /, ,|,/
|
60
|
+
words.reverse.join(' ').gsub(/#{regex}/, '')
|
52
61
|
end
|
53
62
|
end
|
54
63
|
|
data/lib/sankhya/version.rb
CHANGED
data/lib/sankhya.rb
CHANGED
data/spec/sankhya_spec.rb
CHANGED
@@ -66,4 +66,47 @@ describe Sankhya do
|
|
66
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
67
|
end
|
68
68
|
|
69
|
+
# it "converts 22,99,11,123,45,67,890.76 to words" do
|
70
|
+
# (2299111234567890.76).to_words.should eql(["twenty two crore, ninety nine lakh, eleven thousand, one hundred and twenty three crore, forty five lakh, sixty seven thousand, eight hundred and ninety", "seventy six"])
|
71
|
+
# end
|
72
|
+
|
73
|
+
it "converts 21,89,14,967,50,95,052.8 to words" do
|
74
|
+
(2189149675095052.8).to_words.should eql(["twenty one crore, eighty nine lakh, fourteen thousand, nine hundred and sixty seven crore, fifty lakh, ninety five thousand and fifty two", "eighty"])
|
75
|
+
end
|
76
|
+
|
77
|
+
it "converts 10,10,110.10 to words without comma" do
|
78
|
+
(1010110.10).to_words(comma: false).should eql(["ten lakh ten thousand one hundred and ten", "ten"])
|
79
|
+
end
|
80
|
+
|
81
|
+
it "converts 10,10,110.10 to words without comma, if comma option is a truthy value" do
|
82
|
+
(1010110.10).to_words(comma: "false").should eql(["ten lakh, ten thousand, one hundred and ten", "ten"])
|
83
|
+
end
|
84
|
+
|
85
|
+
it "converts 10,10,110.10 to words with scale -2" do
|
86
|
+
(1010110.10).to_words(scale: -2).should eql(["ten lakh, ten thousand, one hundred and ten", "zero"])
|
87
|
+
end
|
88
|
+
|
89
|
+
it "converts 10,10,110.10 to words with scale -1" do
|
90
|
+
(1010110.10).to_words(scale: -1).should eql(["ten lakh, ten thousand, one hundred and ten", "zero"])
|
91
|
+
end
|
92
|
+
|
93
|
+
it "converts 10,10,110.10 to words with scale 0" do
|
94
|
+
(1010110.10).to_words(scale: 0).should eql(["ten lakh, ten thousand, one hundred and ten", "zero"])
|
95
|
+
end
|
96
|
+
|
97
|
+
it "converts 10,10,110.10 to words with scale 2, if scale option is an invalid value" do
|
98
|
+
(1010110.10).to_words(scale: "one").should eql(["ten lakh, ten thousand, one hundred and ten", "ten"])
|
99
|
+
end
|
100
|
+
|
101
|
+
it "converts 10,10,110.10 to words with scale 1" do
|
102
|
+
(1010110.10).to_words(scale: 1).should eql(["ten lakh, ten thousand, one hundred and ten", "one"])
|
103
|
+
end
|
104
|
+
|
105
|
+
it "converts 10,10,110.10 to words with scale 2" do
|
106
|
+
(1010110.10).to_words(scale: 2).should eql(["ten lakh, ten thousand, one hundred and ten", "ten"])
|
107
|
+
end
|
108
|
+
|
109
|
+
it "converts 10,10,110.10 to words with scale 3" do
|
110
|
+
(1010110.10).to_words(scale: 3).should eql(["ten lakh, ten thousand, one hundred and ten", "one hundred"])
|
111
|
+
end
|
69
112
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sankhya
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Geordee Naliyath
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|