如何从下面的散列中打印金额?散列存储在输出变量中。
我只想打印金额"12.37“。
输出= {:next_page_token=>nil, :group_definitions=>nil, :results_by_time=>[{:time_period=>{:start=>"2022-07-01", :end=>"2022-08-01"}, :total=>{"BlendedCost"=>{:amount=>"12.3766372967", :unit=>"USD"}}, :groups=>[], :estimated=>false}], :dimension_value_attributes=>[]}
发布于 2022-08-20 17:02:37
使用Hash#dig并遵循指向值的路径:
output.dig(:results_by_time, 0, :total, 'BlendedCost', :amount)
#=> "12.3766372967"
amount = output.dig(:results_by_time, 0, :total, 'BlendedCost', :amount)
Float(amount).round(2)
#=> 12.38https://stackoverflow.com/questions/73428533
复制相似问题