Skip to content

Latest commit

 

History

History
79 lines (54 loc) · 1.37 KB

README.md

File metadata and controls

79 lines (54 loc) · 1.37 KB

Satoshis

Build Status Maintainability Gem Version

A Ruby gem to manipulate Bitcoin money without loss of precision.

Why?

In simple operations like 0.3 - 0.1 (using Float data types) the result may be a bit strange:

difference = 0.3 - 0.1

puts difference
# 0.19999999999999998

Using the Satoshis gem you get:

difference = Satoshis.new("0.3") - Satoshis.new("0.1")

puts difference.to_s
# "0.2"

And you can do some conversions of bitcoin values:

satoshis = 420000
amount   = Satoshis.new(satoshis)

puts amount.to_s
# "0.0042"
# String

puts amount.to_d
# 0.42e-2
# BigDecimal

puts amount.to_i
# 420000
# Integer (satoshis)

Installation

Add this line to your application's Gemfile:

gem "satoshis"

And then execute:

bundle install

Or install it yourself as:

gem install satoshis

Custom Precision

Use the class CoinDecimal passing the argument precision:

satoshis = 42
amount   = CoinDecimal.new(satoshis, 10)

puts amount.to_s
# "0.0000000042"
# String