所以我正在尝试一些代码来将数字转换为字符串。但是,我注意到在某些情况下,它不会保留最后两位小数。例如,我输入 1.01 和 1.04 进行加法,然后返回 2.04。如果我只输入 1.05 它会保留数字并准确地返回它。我知道发生了什么事情正在四舍五入。我不知道如何防止它被四舍五入。我是否应该只考虑将 (1.01+1.04) 发送给 self 作为一个输入?
警告!我还没有尝试过,所以不知道它是否支持:
user_input = (1.04+1.01) #entry from user
user_input = gets.to_f
user_input.to_test_string
到目前为止我所拥有的:
class Float
def to_test_string
cents = self % 1
dollars = self - cents
cents = cents * 100
text = "#{dollars.to_i.en.numwords} dollars and #{cents.to_i.en.numwords} cents"
puts text
text
end
end
puts "Enter two great floating point numbers for adding"
puts "First number"
c = gets.to_f
puts "Second number"
d = gets.to_f
e = c+d
puts e.to_test_string
puts "Enter a great floating number! Example 10.34"
a = gets.to_f
puts a.to_test_string
谢谢您的帮助!贴一些代码,我可以试试!