【发布时间】:2015-06-09 20:06:21
【问题描述】:
我有一个哈希,其中键是月份,我想将对象转换为正数和货币。
输入
hash = {
12 => -5888.969999999999,
4 => -6346.1,
3 => -6081.76,
2 => -5774.799999999999,
1 => -4454.38
}
输出
hash = {
12 => 5888.96,
4 => 6346.10,
3 => 6081.76,
2 => 5774.79,
1 => 4454.38
}
#Output should be a float
任何帮助将不胜感激。
【问题讨论】:
-
请期待输出
-
您的输出语法无效 - 值是字符串还是 BigDecimals 还是什么?
-
Numeric.abs() 可用于确保数字为正数,而 Float.round(2) 会将浮点数四舍五入到小数点后 2 位。有关用法示例,请参阅 ruby-doc.org/core-2.1.4/Numeric.html#method-i-abs 和 ruby-doc.org/core-2.2.2/Float.html#method-i-round。
-
所以 abs 方法有效,但我不能使用浮点数,因为某些对象已经在小数点后有 1 位数字并且会引发错误
-
我将问题更新为浮点数
标签: ruby-on-rails ruby hash currency