module
Money::Rounding
Direct including types
Defined in:
money/money/rounding.crInstance Method Summary
-
#round(precision : Int = 0, mode : Number::RoundingMode = Money.rounding_mode) : Money
Rounds the monetary amount to smallest unit of coinage, using rounding mode if given, or
Money.rounding_modeotherwise. -
#round_to_nearest_cash_value(rounding_mode : Number::RoundingMode = Money.rounding_mode) : Money
Returns a new
Moneyinstance with the nearest possible amount in cash value (cents), ornilif the#currencyhas no smallest denomination defined. -
#round_to_nearest_cash_value!(rounding_mode : Number::RoundingMode = Money.rounding_mode) : Money
Returns a new
Moneyinstance with the nearest possible amount in cash value (cents), ornilif the#currencyhas no smallest denomination defined. -
#round_to_nearest_cash_value?(rounding_mode : Number::RoundingMode = Money.rounding_mode) : Money | Nil
Returns a new
Moneyinstance with the nearest possible amount in cash value (cents), ornilif the#currencyhas no smallest denomination defined.
Instance Method Detail
Rounds the monetary amount to smallest unit of coinage, using
rounding mode if given, or Money.rounding_mode otherwise.
Money.new(10.1, "USD").round # => Money(@amount=10.0, @currency="USD")
Money.new(10.5, "USD").round(mode: :ties_even) # => Money(@amount=10.0, @currency="USD")
Money.new(10.5, "USD").round(mode: :ties_away) # => Money(@amount=11.0, @currency="USD")
Returns a new Money instance with the nearest possible amount in cash value
(cents), or nil if the #currency has no smallest denomination defined.
For example, in Swiss franc (CHF), the smallest possible amount of cash value is CHF 0.05. Therefore, for CHF 0.07 this method returns CHF 0.05, and for CHF 0.08, CHF 0.10.
Money.new(0.07, "CHF").round_to_nearest_cash_value? # => Money(@amount = 0.05)
Money.new(0.08, "CHF").round_to_nearest_cash_value? # => Money(@amount = 0.1)
Money.new(10.0, "XAG").round_to_nearest_cash_value? # nil
NOTE This variant returns self if #round_to_nearest_cash_value?
returns nil.
Returns a new Money instance with the nearest possible amount in cash value
(cents), or nil if the #currency has no smallest denomination defined.
For example, in Swiss franc (CHF), the smallest possible amount of cash value is CHF 0.05. Therefore, for CHF 0.07 this method returns CHF 0.05, and for CHF 0.08, CHF 0.10.
Money.new(0.07, "CHF").round_to_nearest_cash_value? # => Money(@amount = 0.05)
Money.new(0.08, "CHF").round_to_nearest_cash_value? # => Money(@amount = 0.1)
Money.new(10.0, "XAG").round_to_nearest_cash_value? # nil
NOTE This variant raises UndefinedSmallestDenominationError if
#round_to_nearest_cash_value? returns nil.
Returns a new Money instance with the nearest possible amount in cash value
(cents), or nil if the #currency has no smallest denomination defined.
For example, in Swiss franc (CHF), the smallest possible amount of cash value is CHF 0.05. Therefore, for CHF 0.07 this method returns CHF 0.05, and for CHF 0.08, CHF 0.10.
Money.new(0.07, "CHF").round_to_nearest_cash_value? # => Money(@amount = 0.05)
Money.new(0.08, "CHF").round_to_nearest_cash_value? # => Money(@amount = 0.1)
Money.new(10.0, "XAG").round_to_nearest_cash_value? # nil