为什么我的销售税计算器不起作用?

时间:2015-02-04 02:40:58

标签: ruby

puts "Hello, welcome to the sales tax calculator"
puts "What is your total price?"
price = gets.chomp
tax = 0.0635 * price
total = tax + price
puts "#{total} is your price including sales tax"

它应该采取价格并增加销售税。对不起,Ruby noob。

1 个答案:

答案 0 :(得分:1)

当您从提示输入值时,它将以字符串形式出现。您希望使用String#to_f将其转换为浮点数。

例如:

price = gets.to_f

如果您给出了提示10.0,它会将其转换为:

[16] pry(main)> "10.0\n".to_f
=> 10.0

chomp似乎没有必要这样做,只需调用to_f即可删除换行符,这是我刚刚学到的内容!

相关问题