How can print the amount from the following hash? The hash is stored inside a output variable.
And I want to print only the amount "12.37".
output = {: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=>[]}
CodePudding user response:
Use Hash#dig
and follow the path to the value:
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.38