Class: BTC::CurrencyFormatter
- Inherits:
-
Object
- Object
- BTC::CurrencyFormatter
- Defined in:
- lib/btcruby/currency_formatter.rb
Overview
Modeled after NSNumberFormatter in Cocoa, this class allows to convert bitcoin amounts to their string representations and vice versa.
Constant Summary collapse
- STYLE_BTC =
1.0 is 1 btc (100_000_000 satoshis)
:btc- STYLE_BTC_LONG =
1.00000000 is 1 btc (100_000_000 satoshis)
:btc_long- STYLE_MBTC =
1.0 is 0.001 btc (100_000 satoshis)
:mbtc- STYLE_BIT =
1.0 is 0.000001 btc (100 satoshis)
:bit- STYLE_SATOSHIS =
1.0 is 0.00000001 btc (1 satoshi)
:satoshis
Instance Attribute Summary collapse
-
#show_suffix ⇒ Object
Returns the value of attribute show_suffix.
-
#style ⇒ Object
Returns the value of attribute style.
Class Method Summary collapse
-
.btc_formatter ⇒ Object
Returns a singleton formatter for BTC values (1.0 is one bitcoin) without suffix.
-
.btc_long_formatter ⇒ Object
Returns a singleton formatter for BTC values where there are always 8 places after decimal point (e.g. “42.00000000”).
Instance Method Summary collapse
-
#dup ⇒ Object
Creates a copy if you want to customize another formatter (e.g. a global singleton like btc_formatter).
-
#initialize(style: :btc, show_suffix: false) ⇒ CurrencyFormatter
constructor
A new instance of CurrencyFormatter.
-
#number_from_string(string) ⇒ Object
Returns amount of satoshis parsed from a formatted string according to style attribute.
-
#string_from_number(number) ⇒ Object
Returns formatted string for an amount in satoshis.
Constructor Details
#initialize(style: :btc, show_suffix: false) ⇒ CurrencyFormatter
Returns a new instance of CurrencyFormatter.
28 29 30 31 |
# File 'lib/btcruby/currency_formatter.rb', line 28 def initialize(style: :btc, show_suffix: false) @style = style @show_suffix = show_suffix end |
Instance Attribute Details
#show_suffix ⇒ Object
Returns the value of attribute show_suffix.
15 16 17 |
# File 'lib/btcruby/currency_formatter.rb', line 15 def show_suffix @show_suffix end |
#style ⇒ Object
Returns the value of attribute style.
14 15 16 |
# File 'lib/btcruby/currency_formatter.rb', line 14 def style @style end |
Class Method Details
.btc_formatter ⇒ Object
Returns a singleton formatter for BTC values (1.0 is one bitcoin) without suffix.
18 19 20 |
# File 'lib/btcruby/currency_formatter.rb', line 18 def self.btc_formatter @btc_formatter ||= self.new(style: STYLE_BTC) end |
.btc_long_formatter ⇒ Object
Returns a singleton formatter for BTC values where there are always 8 places after decimal point (e.g. “42.00000000”).
24 25 26 |
# File 'lib/btcruby/currency_formatter.rb', line 24 def self.btc_long_formatter @btc_long_formatter ||= self.new(style: STYLE_BTC_LONG) end |
Instance Method Details
#dup ⇒ Object
Creates a copy if you want to customize another formatter (e.g. a global singleton like btc_formatter)
59 60 61 |
# File 'lib/btcruby/currency_formatter.rb', line 59 def dup self.class.new(style: @style, show_suffix: @show_suffix) end |
#number_from_string(string) ⇒ Object
Returns amount of satoshis parsed from a formatted string according to style attribute.
48 49 50 51 52 53 54 55 56 |
# File 'lib/btcruby/currency_formatter.rb', line 48 def number_from_string(string) bd = BigDecimal.new(string) if @style == :btc || @style == :btc_long return (bd * BTC::COIN).to_i else # TODO: support other styles raise "Not Implemented" end end |
#string_from_number(number) ⇒ Object
Returns formatted string for an amount in satoshis.
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/btcruby/currency_formatter.rb', line 34 def string_from_number(number) if @style == :btc number = number.to_i return "#{number / BTC::COIN}.#{'%08d' % [number % BTC::COIN]}".gsub(/0+$/,"").gsub(/\.$/,".0") elsif @style == :btc_long number = number.to_i return "#{number / BTC::COIN}.#{'%08d' % [number % BTC::COIN]}" else # TODO: parse other styles raise "Not implemented" end end |