Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/satoshi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def to_i
return 0 if @value.nil?
@value
end
alias :satoshi_value :to_i
alias :satoshi_value :to_i

def to_s
to_unit.to_s
Expand Down Expand Up @@ -110,7 +110,7 @@ def coerce(other)
end

private

def to_denomination(digits_after_delimiter, as: :number)
sign = @value < 0 ? -1 : 1
val = @value.abs
Expand All @@ -122,7 +122,7 @@ def to_denomination(digits_after_delimiter, as: :number)
result.reverse!
result = result.sub(/\A0*/, '').sub(/0*\Z/, '') # remove zeros on both sides
if as == :number
result.to_f*sign
result.to_f*sign
else
if result == '.'
result = '0.0'
Expand All @@ -136,16 +136,16 @@ def to_denomination(digits_after_delimiter, as: :number)
def convert_to_satoshi(n)
n = BigDecimal.new(n.to_s)

decimal_part_length = n.to_s("F").split(".")[1]
decimal_part_length = if decimal_part_length
decimal_part = n.to_s("F").split(".")[1]
decimal_part_length = if decimal_part
decimal_part_length.sub(/0*\Z/, "").length
else
0
end

if decimal_part_length > UNIT_DENOMINATIONS[@from_unit]
raise TooManyDigitsAfterDecimalPoint,
"Too many digits (#{decimal_part_length}) after decimal point used for #{@from_unit} value, while #{UNIT_DENOMINATIONS[@from_unit]} allowed"
"Too many digits (#{decimal_part_length}) after decimal point used for #{@from_unit} value, while #{UNIT_DENOMINATIONS[@from_unit]} allowed"
end

n = ("%.#{UNIT_DENOMINATIONS[@from_unit]}f" % n) # otherwise we might see a scientific notation
Expand Down