开发者

Ruby: If I wanted to convert order.price to a decimal value, do I use order.price.to_i?

开发者 https://www.devze.com 2022-12-08 06:48 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消