我多年来一直在努力。我正在尝试使用哈希值对数组进行舍入。
[
{
:email => "user_01@yorlook.com",
:amount => 129.22500000000002
},
{
:email => "user_02@yorlook.com",
:amount => 112.67500000000001
}
]
我需要一种方法让它看起来像这样:
[
{
:email => "user_01@yorlook.com",
:amount => 129.23
},
{
:email => "user_02@yorlook.com",
:amount => 112.68
}
]
提前感谢您的帮助!
答案 0 :(得分:4)
假设arr
始终包含Hash
,密钥amount
为Float
(如示例所示)。
arr.each { |hash|
hash[:amount] = hash[:amount].round(2)
}
#=> [{:email=>"user_01@yorlook.com", :amount=>129.23}, {:email=>"user_02@yorlook.com", :amount=>112.68}]