Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何获得金钱的绝对价值?例如:
-2.to_money.? = 2.to_money 2.to_money.? = 2.to_money
我有一个属性total_price,它可能是正数或负数。我想计算 total_price 的绝对值。
尝试在转换为货币之前取绝对值。
2.abs.to_money
现在 Money::Arithmetic 模块内置了一个绝对值方法:
2.to_money.abs == #<Money fractional:200 currency:USD> # true -2.to_money.abs == #<Money fractional:200 currency:USD> # true