splittable 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/Gemfile.lock +1 -1
- data/README.md +21 -0
- data/lib/splittable.rb +4 -4
- data/lib/splittable/division.rb +5 -4
- data/lib/splittable/normalize.rb +3 -3
- data/lib/splittable/version.rb +1 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ad6faa4d272be234b860ea39ab7da30ec46b101ee1d53f3e452d332c13c13104
|
4
|
+
data.tar.gz: af7cd7ea65c72c019fbaf543e371ad6ecc4a184707ce5cd73e2779166f69d519
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a9052a638a0e6295c13719d42356bbde2d1276da72d948fb728b9400fac254dc638680fda92f33ccc57b2f778318b9f1be8b3740b05af57a91005582281e906e
|
7
|
+
data.tar.gz: 3d6fce296eefb776df9994d8c68beaca1d436e5b30840b785a82ac5e2e6579f420376f9f28d2cd1dff68cd3ec517348ccc0f62a7334659994d82caf5afa39ee5
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -36,6 +36,17 @@ Result: the total truncated value was divided by the number of plots informed an
|
|
36
36
|
```ruby
|
37
37
|
=> [0.5e-1, 0.3e-1, 0.3e-1] # => [0.05, 0.03, 0.03]
|
38
38
|
```
|
39
|
+
Default precision is 2 decimal places, but, you can customize this with precision parameter:
|
40
|
+
|
41
|
+
``` ruby
|
42
|
+
Splittable.division(value: 10, quantity: 3, precision: 3)
|
43
|
+
```
|
44
|
+
|
45
|
+
Result:
|
46
|
+
```ruby
|
47
|
+
=> [0.3334e1, 0.3333e1, 0.3333e1] # => [0.334, 0.333, 0.333]
|
48
|
+
```
|
49
|
+
|
39
50
|
|
40
51
|
Using `normalize` method:
|
41
52
|
|
@@ -49,6 +60,16 @@ Result: all values are truncated and them the difference is attributed in the fi
|
|
49
60
|
=> [0.3524e2, 0.2143e2, 0.4333e2] # => [35.24, 21.43, 43.33]
|
50
61
|
```
|
51
62
|
|
63
|
+
In this method, you have the same optional precision parameter:
|
64
|
+
```ruby
|
65
|
+
Splittable.normalize(value: 100, installments: [33.33333333, 33.33333333, 33.33333333], precision: 3)
|
66
|
+
```
|
67
|
+
|
68
|
+
Result:
|
69
|
+
```ruby
|
70
|
+
=> [0.33334e2, 0.33333e2, 0.33333e2] # => [33.334, 33.333, 33.333]
|
71
|
+
```
|
72
|
+
|
52
73
|
## Development
|
53
74
|
|
54
75
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/splittable.rb
CHANGED
@@ -9,14 +9,14 @@ module Splittable
|
|
9
9
|
class << self
|
10
10
|
# receive total value and to quantity installments are required to equal division
|
11
11
|
# just the first installment will receive the difference cents
|
12
|
-
def division(value:, quantity:)
|
13
|
-
Splittable::Division.new(value: value, quantity: quantity).call
|
12
|
+
def division(value:, quantity:, precision: 2)
|
13
|
+
Splittable::Division.new(value: value, quantity: quantity, precision: precision).call
|
14
14
|
end
|
15
15
|
|
16
16
|
# receive total value and specific value of installments are required to specific division
|
17
17
|
# just the first installment will receive the difference cents
|
18
|
-
def normalize(value:, installments:)
|
19
|
-
Splittable::Normalize.new(value: value, installments: installments).call
|
18
|
+
def normalize(value:, installments:, precision: 2)
|
19
|
+
Splittable::Normalize.new(value: value, installments: installments, precision: precision).call
|
20
20
|
end
|
21
21
|
end
|
22
22
|
end
|
data/lib/splittable/division.rb
CHANGED
@@ -1,15 +1,16 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class Splittable::Division
|
4
|
-
def initialize(value:, quantity:)
|
5
|
-
@
|
4
|
+
def initialize(value:, quantity:, precision: 2)
|
5
|
+
@precision = precision
|
6
|
+
@value = BigDecimal(value, 15).truncate(precision)
|
6
7
|
@quantity = BigDecimal(quantity.to_i, 15)
|
7
8
|
|
8
9
|
check_quantity_as_positive_value!
|
9
10
|
end
|
10
11
|
|
11
12
|
def call
|
12
|
-
partial_value = (value / quantity).truncate(
|
13
|
+
partial_value = (value / quantity).truncate(precision)
|
13
14
|
installments = [partial_value] * quantity
|
14
15
|
installments[0] += value - installments.sum.to_d
|
15
16
|
|
@@ -18,7 +19,7 @@ class Splittable::Division
|
|
18
19
|
|
19
20
|
private
|
20
21
|
|
21
|
-
attr_reader :value, :quantity
|
22
|
+
attr_reader :value, :quantity, :precision
|
22
23
|
|
23
24
|
def check_quantity_as_positive_value!
|
24
25
|
return if quantity.positive?
|
data/lib/splittable/normalize.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class Splittable::Normalize
|
4
|
-
def initialize(value:, installments:)
|
5
|
-
@value = BigDecimal(value, 15).truncate(
|
6
|
-
@installments = installments.map { |installment| BigDecimal(installment.round(
|
4
|
+
def initialize(value:, installments:, precision: 2)
|
5
|
+
@value = BigDecimal(value, 15).truncate(precision)
|
6
|
+
@installments = installments.map { |installment| BigDecimal(installment.round(precision), 15) }
|
7
7
|
end
|
8
8
|
|
9
9
|
def call
|
data/lib/splittable/version.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: splittable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Arthur Brandão
|
8
8
|
- Marcelo Toledo
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2021-02-
|
12
|
+
date: 2021-02-02 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: |-
|
15
15
|
This gem solves the problem of several decimal places in divisions
|
@@ -56,7 +56,7 @@ metadata:
|
|
56
56
|
homepage_uri: https://github.com/Pagnet/splittable
|
57
57
|
source_code_uri: https://github.com/Pagnet/splittable
|
58
58
|
changelog_uri: https://github.com/Pagnet/splittable/blob/master/CHANGELOG.md
|
59
|
-
post_install_message:
|
59
|
+
post_install_message:
|
60
60
|
rdoc_options: []
|
61
61
|
require_paths:
|
62
62
|
- lib
|
@@ -71,8 +71,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
71
71
|
- !ruby/object:Gem::Version
|
72
72
|
version: '0'
|
73
73
|
requirements: []
|
74
|
-
rubygems_version: 3.
|
75
|
-
signing_key:
|
74
|
+
rubygems_version: 3.2.3
|
75
|
+
signing_key:
|
76
76
|
specification_version: 4
|
77
77
|
summary: Calculate division and normalize parcels to use just cents.
|
78
78
|
test_files: []
|