sankhya 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1d2e0c7d806546a25964eb799e9ac93539551e28
4
- data.tar.gz: ae607af6d1fd20ab6502d7d2b5dbb1290810b3cd
3
+ metadata.gz: 78a4f985011da8598c53c68f0d3b0811f717d50c
4
+ data.tar.gz: 553f1c68691e7e7a8413cf5ad1d2c42394ce8cf7
5
5
  SHA512:
6
- metadata.gz: 29322362d617f075f04b66026f628b0ae277d305a7ec7dc4cebb231781cb061cab0f17e13169e92c17e730832c8183d9c299eedfab5a5b62b76635f914b71cd7
7
- data.tar.gz: 08506902eeebf36dea735e625e4461df50649f8df502f5cc3539e52632a84ba0084ef50f9432a3d09ae6393be8dd0b91602be5ad0a09ea535b8485cb5ec7fedd
6
+ metadata.gz: 4b5db5eb2714e808c586fc155028717c2041e6044c32ce0400f1fcd1e8aed684f7fe2784fa69b4488c1ea98ac6c75fb568416c8d94a0e101f8fa9e13ebb1a01d
7
+ data.tar.gz: 6f29b000505cdf23733284b73d720fb28e3c05be137498d6cb281dca9234d2878d0b0b1f528234d558ba818c0a5eb11f8d56e208ed976bd681d719fcdf80a58d
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sankhya (0.1.0)
4
+ sankhya (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -3,21 +3,29 @@ module Sankhya
3
3
  class Numbers
4
4
  extend Words
5
5
 
6
- def self.translate(number, scale)
7
- scale ||= 2
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
- # 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)]
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
- words.reverse.join(' ').gsub(/, ,|,$/, '')
59
+ regex = @comma ? /, ,|,$/ : /, ,|,/
60
+ words.reverse.join(' ').gsub(/#{regex}/, '')
52
61
  end
53
62
  end
54
63
 
@@ -1,3 +1,3 @@
1
1
  module Sankhya
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/sankhya.rb CHANGED
@@ -4,7 +4,7 @@ require "sankhya/numbers"
4
4
 
5
5
  module Sankhya
6
6
  def to_words(options = {})
7
- Numbers::translate self, options[:scale]
7
+ Numbers::translate self, options
8
8
  end
9
9
  end
10
10
 
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.1.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-18 00:00:00.000000000 Z
11
+ date: 2014-02-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler