Ruby 四舍五入到最接近的数字
结束Ruby 四舍五入到最接近的数字,ruby,rounding,Ruby,Rounding,结束 现在,代码将告诉您最大的\u数比最小的\u数多出多少百分比。但问题是它打印出了一长串小数,这是一个很难排序的问题。因此,如果我想让代码只显示前3个数字,我会怎么做???你想使用的是total\u percent\u更多。四舍五入像这样: def percent_more puts "What is the biggest number?" biggest_number = gets.chomp puts "What is the smallest number?" smallest_num
现在,代码将告诉您最大的\u数比最小的\u数多出多少百分比。但问题是它打印出了一长串小数,这是一个很难排序的问题。因此,如果我想让代码只显示前3个数字,我会怎么做???你想使用的是
total\u percent\u更多。四舍五入像这样:
def percent_more
puts "What is the biggest number?"
biggest_number = gets.chomp
puts "What is the smallest number?"
smallest_number = gets.chomp
difference = biggest_number.to_i - smallest_number.to_i
total_percent_more = difference / smallest_number.to_f
puts "Your biggest number is #{total_percent_more}% bigger then your smallest number. Don't forget to round off to the nearest whole percent!"
有关更多信息,请参阅文档:
在1.9之前的ruby版本中,您需要像这样使用sprintf:
puts "What is the biggest number?"
biggest_number = gets.chomp
puts "What is the smallest number?"
smallest_number = gets.chomp
difference = biggest_number.to_i - smallest_number.to_i
total_percent_more = difference / smallest_number.to_f
puts "Your biggest number is #{total_percent_more.round}% bigger then your smallest number. Don't forget to round off to the nearest whole percent!"
您可以通过更改数字来更改小数位数
有关更多详细信息,请参阅文档:
您想要使用的是更多的total\u percent\u。如下所示:
def percent_more
puts "What is the biggest number?"
biggest_number = gets.chomp
puts "What is the smallest number?"
smallest_number = gets.chomp
difference = biggest_number.to_i - smallest_number.to_i
total_percent_more = difference / smallest_number.to_f
puts "Your biggest number is #{total_percent_more}% bigger then your smallest number. Don't forget to round off to the nearest whole percent!"
有关更多信息,请参阅文档:
在1.9之前的ruby版本中,您需要像这样使用sprintf:
puts "What is the biggest number?"
biggest_number = gets.chomp
puts "What is the smallest number?"
smallest_number = gets.chomp
difference = biggest_number.to_i - smallest_number.to_i
total_percent_more = difference / smallest_number.to_f
puts "Your biggest number is #{total_percent_more.round}% bigger then your smallest number. Don't forget to round off to the nearest whole percent!"
您可以通过更改数字来更改小数位数
有关更多详细信息,请参阅文档:
下面是一个如何四舍五入到小数点后2位的示例
result = 10/6.0
puts result
printf("%.3f\n", result)
--output:--
1.66666666666667
1.667
如果您希望四舍五入到最接近的3位小数,则类似于:
amount = 342
puts amount.round(2)
下面是一个如何四舍五入到小数点后2位的示例
result = 10/6.0
puts result
printf("%.3f\n", result)
--output:--
1.66666666666667
1.667
如果您希望四舍五入到最接近的3位小数,则类似于:
amount = 342
puts amount.round(2)
看。@CarySwoveland我试着用15/60.round(2)输入,但结果是0。也许你的意思是(15/60)。round(2)=>0.0
,这是你应该得到的,因为15/60=>0
,所以这就是0.round(2)=>0.0
。试试这个:(15.0/60)。四舍五入(2)=>0.25
,这与(15.to\u f/60)相同。四舍五入(2)=>0.25
。如果分子或分母是浮点数,则计算出的比率是浮点数<代码>15/60。圆形(2)=>15/(60.round(2))=>15/60.0=>0.25
。看。@CarySwoveland我在15/60中输入了这一点。圆形(2)但结果是零也许你的意思是(15/60)。圆形(2)=>0.0
,这是你应该得到的,因为15/60=>0
,所以这就是0.round(2)=>0.0
。试试这个:(15.0/60)。四舍五入(2)=>0.25
,这与(15.to\u f/60)相同。四舍五入(2)=>0.25
。如果分子或分母是浮点数,则计算出的比率是浮点数<代码>15/60。轮数(2)=>15/(60.轮数(2))=>15/60.0=>0.25
。我用数字5和45试过,但结果仍然是0。我在寻找答案0.11你会使用{total_percent_more.round 2}
将其保留到小数点后2位。你使用的是什么版本的ruby,因为我不知道它说的是什么错误版本1.8.7。。。该函数仅舍入到1.8.7中最接近的整数。您需要像使用sprintf一样使用sprintfsprintf('%.2f',总百分比更高)
。我将更新我的答案以包含它。我尝试使用数字5和45,但结果仍然是0。我在寻找答案0.11你会使用{total_percent_more.round 2}
将其保留到小数点后2位。你使用的是什么版本的ruby,因为我不知道它说的是什么错误版本1.8.7。。。该函数仅舍入到1.8.7中最接近的整数。您需要像使用sprintf一样使用sprintfsprintf('%.2f',总百分比更高)
。我将更新我的答案以包含它。