to_d to always return 2 decimals places in ruby

后端 未结 5 1912
你的背包
你的背包 2020-12-08 06:01

I\'m dealing with currencies and I want to round down the number to 2 decimal places. Even if the number is 500.0, I would like it to be 500.00 to be consistent. When I do \

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-08 06:47

    In addition to mcfinnigan's answer, you can also use the following to get 2 decimal places

    '%.2f' % 500 # "500.00"
    

    This use case is known as the string format operator

提交回复
热议问题