Class: Mint::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/mint/money/utils.rb

Overview

Special utilities

Class Method Summary collapse

Class Method Details

.to_amount(value, currency) ⇒ BigDecimal

Normalize and guess amount’s type

Examples:

'1.178' -> <BigDecimal: 1.18>

Parameters:

  • value (BigDecimal)
  • currency (Symbol)

Returns:

  • (BigDecimal)


26
27
28
# File 'lib/mint/money/utils.rb', line 26

def to_amount(value, currency)
  to_bigdecimal(value).round(precision(currency))
end

.to_format(value, currency) ⇒ String

Format number as string with correct precision

Parameters:

  • value (BigDecimal)
  • currency (Symbol)

Returns:

  • (String)


34
35
36
# File 'lib/mint/money/utils.rb', line 34

def to_format(value, currency)
  format("%.#{precision(currency)}f", value.to_f)
end

.to_key(key) ⇒ Symbol

Normalize string to our keys format

Examples:

'usd' -> :USD

Parameters:

  • (String, Symbol)

Returns:

  • (Symbol)


16
17
18
# File 'lib/mint/money/utils.rb', line 16

def to_key(key)
  key.to_s.upcase.to_sym
end