本文介绍了为什么 Ruby 中的除法返回整数而不是十进制值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如:
9 / 5 #=> 1
但我期望 1.8
.如何获得正确的十进制(非整数)结果?为什么它完全返回 1
?
but I expected 1.8
. How can I get the correct decimal (non-integer) result? Why is it returning 1
at all?
推荐答案
它正在做整数除法.您可以将其中一个数字设为 Float
通过添加 .0
:
It’s doing integer division. You can make one of the numbers a Float
by adding .0
:
9.0 / 5 #=> 1.8
9 / 5.0 #=> 1.8
这篇关于为什么 Ruby 中的除法返回整数而不是十进制值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!