paisa 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +2 -0
- data/README.md +21 -6
- data/circle.yml +3 -0
- data/lib/paisa.rb +83 -9
- data/lib/paisa/version.rb +1 -1
- data/paisa.gemspec +1 -0
- metadata +18 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: bb609acdf81fbd52d8c95b642f1d519509c7cd3a9e15c67d021a117af8dd3309
|
4
|
+
data.tar.gz: e541890f38dd6d9fe7ce17ee57d783c44ef953a74d0b11af4920f481a7b01f5e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 01a60662b1531db871b56c9b7b025324456acc66c90c131e60e5d5792b995200b44a85aa559c006fd6d26e634fc5119c09c78d516c14965865dda3520011b54a
|
7
|
+
data.tar.gz: c1cd1d08599c7fc45944042db35e221ec559f5660284c6b87e77e8dbbb35c518e982d8ed2bdf510455d729f9f9af9d1f5b4601c2a9b11d59f2857378fc5e4c85
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
|
-
# Paisa
|
1
|
+
# Paisa
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
Having trouble figuring out where to put the commas in Indian money? Are you being lazy and defaulting to hundreds of thousands and millions instead of lakhs and crores? Default no more! `Paisa` is here to save you.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -22,7 +20,24 @@ Or install it yourself as:
|
|
22
20
|
|
23
21
|
## Usage
|
24
22
|
|
25
|
-
|
23
|
+
```
|
24
|
+
|
25
|
+
// Format paise - if you have your money in Rupees, multiply by 100 to get the number of paise.
|
26
|
+
Paisa.format(12345) // "123.45"
|
27
|
+
Paisa.format(12345678) // "1,23,456.78"
|
28
|
+
Paisa.format(12345678987) // "12,34,56,789.87"
|
29
|
+
Paisa.format(10000) // "100"
|
30
|
+
|
31
|
+
// Use formatWithSymbol to get Paisa to add the official Rupee symbol for you
|
32
|
+
Paisa.format_with_sym(12345678) // "₹1,23,456.78"
|
33
|
+
Paisa.format_with_sym(10000) // "₹100"
|
34
|
+
|
35
|
+
// Pass an optional second parameter to force decimal precision to be set.
|
36
|
+
Paisa.format(12345678987, precision: 0) // "12,34,56,789"
|
37
|
+
Paisa.format(10000, precision: 2) // "100.00"
|
38
|
+
Paisa.format_with_sym(10000, precision: 2) // "₹100.00"
|
39
|
+
|
40
|
+
```
|
26
41
|
|
27
42
|
## Development
|
28
43
|
|
@@ -32,7 +47,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
32
47
|
|
33
48
|
## Contributing
|
34
49
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/paisa. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
50
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/paisa.rb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
51
|
|
37
52
|
|
38
53
|
## License
|
data/circle.yml
ADDED
data/lib/paisa.rb
CHANGED
@@ -1,20 +1,94 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'paisa/version'
|
2
4
|
|
3
5
|
module Paisa
|
6
|
+
SYMBOL = '₹'.freeze
|
7
|
+
ONES = {
|
8
|
+
1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five',
|
9
|
+
6 => 'six', 7 => 'seven', 8 => 'eight', 9 => 'nine', 0 => nil
|
10
|
+
}.freeze
|
11
|
+
|
12
|
+
def self.tens_hash(tens_name)
|
13
|
+
Hash.new do |_, k|
|
14
|
+
[tens_name, ONES[k]].compact.join(' ')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private_class_method :tens_hash
|
19
|
+
|
20
|
+
TENS = {
|
21
|
+
1 => {
|
22
|
+
1 => 'eleven', 2 => 'twelve', 3 => 'thirteen', 4 => 'fourteen', 5 => 'fifteen',
|
23
|
+
6 => 'sixteen', 7 => 'seventeen', 8 => 'eighteen', 9 => 'nineteen', 0 => 'ten'
|
24
|
+
},
|
25
|
+
2 => tens_hash('twenty'),
|
26
|
+
3 => tens_hash('thirty'),
|
27
|
+
4 => tens_hash('forty'),
|
28
|
+
5 => tens_hash('fifty'),
|
29
|
+
6 => tens_hash('sixty'),
|
30
|
+
7 => tens_hash('seventy'),
|
31
|
+
8 => tens_hash('eighty'),
|
32
|
+
9 => tens_hash('ninety'),
|
33
|
+
0 => ONES
|
34
|
+
}.freeze
|
35
|
+
|
4
36
|
def self.format(paise, precision: 2)
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
[rupees, paise.join.rjust(2, '0').slice(0, precision)].reject(&:empty?).join('.')
|
37
|
+
rupee_parts, paise_part = parse(paise)
|
38
|
+
[
|
39
|
+
rupee_parts.join(',').rjust(1, '0'),
|
40
|
+
paise_part.rjust(2, '0').slice(0, precision)
|
41
|
+
].reject(&:empty?).join('.')
|
11
42
|
end
|
12
43
|
|
13
44
|
def self.format_with_sym(paise, precision: 2)
|
14
|
-
[
|
45
|
+
[SYMBOL, format(paise, precision: precision)].join
|
15
46
|
end
|
16
47
|
|
17
|
-
def self.
|
18
|
-
|
48
|
+
def self.words(paise)
|
49
|
+
rupee_parts, paise_part = parse(paise)
|
50
|
+
rupee_text_parts = rupee_parts.reverse.each_with_object([]) do |section, memo|
|
51
|
+
label = ['', ' thousand', ' lakh', ' crore'][memo.size]
|
52
|
+
memo << if section.to_i.zero?
|
53
|
+
nil
|
54
|
+
else
|
55
|
+
[to_words(section), label].join
|
56
|
+
end
|
57
|
+
end
|
58
|
+
text = []
|
59
|
+
text << [rupee_text_parts.to_a.compact.reverse.join(', '), 'rupees'].join(' ') unless rupee_text_parts.empty?
|
60
|
+
text << [to_words(paise_part), 'paise'].join(' ') unless paise_part.to_i.zero?
|
61
|
+
text.join(', ')
|
19
62
|
end
|
63
|
+
|
64
|
+
def self.parse(paise)
|
65
|
+
little_endian = paise.to_s.reverse.chars
|
66
|
+
paise_part = little_endian.shift(2).reverse.join
|
67
|
+
rupee_parts = []
|
68
|
+
cycle = [3, 2, 2].cycle
|
69
|
+
until little_endian.empty?
|
70
|
+
rupee_parts.unshift(little_endian.shift(cycle.next).reverse.join)
|
71
|
+
end
|
72
|
+
[rupee_parts, paise_part]
|
73
|
+
end
|
74
|
+
private_class_method :parse
|
75
|
+
|
76
|
+
def self.to_words(section)
|
77
|
+
digits = section.chars.map(&:to_i)
|
78
|
+
case digits.size
|
79
|
+
when 1
|
80
|
+
ONES.dig(*digits)
|
81
|
+
when 2
|
82
|
+
TENS.dig(*digits)
|
83
|
+
when 3
|
84
|
+
parts = []
|
85
|
+
parts << [ONES[digits[0]], 'hundred'].join(' ')
|
86
|
+
parts << TENS.dig(*digits.slice(1, 2)) unless digits.slice(1, 2).all?(&:zero?)
|
87
|
+
parts.join(' and ')
|
88
|
+
else
|
89
|
+
# do nothing
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
private_class_method :to_words
|
20
94
|
end
|
data/lib/paisa/version.rb
CHANGED
data/paisa.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: paisa
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sudhir Jonathan
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-03-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
description: India specifc formatting for currecies stored in Paise
|
56
70
|
email:
|
57
71
|
- sudhir.j@gmail.com
|
@@ -68,6 +82,7 @@ files:
|
|
68
82
|
- Rakefile
|
69
83
|
- bin/console
|
70
84
|
- bin/setup
|
85
|
+
- circle.yml
|
71
86
|
- lib/paisa.rb
|
72
87
|
- lib/paisa/version.rb
|
73
88
|
- paisa.gemspec
|
@@ -91,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
91
106
|
version: '0'
|
92
107
|
requirements: []
|
93
108
|
rubyforge_project:
|
94
|
-
rubygems_version: 2.
|
109
|
+
rubygems_version: 2.7.3
|
95
110
|
signing_key:
|
96
111
|
specification_version: 4
|
97
112
|
summary: India specifc formatting for currecies stored in Paise
|