Ruby: If I wanted to convert order.price to a decimal value, do I use order.price.to_i?
Very beginner's question: I noticed that there's no .to_d (such as order.price.to_d to convert order.price into开发者_高级运维 a decimal). Say order.price is currently a string; can I then use order.price.to_i to convert it into a decimal?
The Ruby corelib documentation is your friend!
>> "9.95".to_f
=> 9.95
>> "9.95".to_f.class
=> Float
>>
I think you'll want to use BigDecimal instead of Float if you plan on doing any calculations with the price. It retains the precision and isn't subject to the rounding errors that float is.
精彩评论