module Money::Rounding

Direct including types

Defined in:

money/money/rounding.cr

Instance Method Summary

Instance Method Detail

def 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_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")

[View source]
def rounded_to_nearest_cash_value(rounding_mode : Number::RoundingMode = Money.rounding_mode) : Money #

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").rounded_to_nearest_cash_value? # => Money(@amount = 0.05)
Money.new(0.08, "CHF").rounded_to_nearest_cash_value? # => Money(@amount = 0.1)
Money.new(10.0, "XAG").rounded_to_nearest_cash_value? # nil

NOTE This variant returns self if #rounded_to_nearest_cash_value? returns nil.


[View source]
def rounded_to_nearest_cash_value!(rounding_mode : Number::RoundingMode = Money.rounding_mode) : Money #

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").rounded_to_nearest_cash_value? # => Money(@amount = 0.05)
Money.new(0.08, "CHF").rounded_to_nearest_cash_value? # => Money(@amount = 0.1)
Money.new(10.0, "XAG").rounded_to_nearest_cash_value? # nil

NOTE This variant raises UndefinedSmallestDenominationError if #rounded_to_nearest_cash_value? returns nil.


[View source]
def rounded_to_nearest_cash_value?(rounding_mode : Number::RoundingMode = Money.rounding_mode) : Money | 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").rounded_to_nearest_cash_value? # => Money(@amount = 0.05)
Money.new(0.08, "CHF").rounded_to_nearest_cash_value? # => Money(@amount = 0.1)
Money.new(10.0, "XAG").rounded_to_nearest_cash_value? # nil

[View source]