Search code examples
ruby-on-rails-2currency

Rails 2 - Absolute of money


How to get absolute value of money? e.g:

-2.to_money.? = 2.to_money
 2.to_money.? = 2.to_money

I have an attribute total_price, which may be positive or negative. I want to calculate the absolute value of total_price.


Solution

  • There's now an absolute value method built-in to the Money::Arithmetic module:

    2.to_money.abs == #<Money fractional:200 currency:USD> # true
    -2.to_money.abs == #<Money fractional:200 currency:USD> # true