检查 Float 是否等效于 Ruby 中的整数值 [英] Checking if a Float is equivalent to an integer value in Ruby
本文介绍了检查 Float 是否等效于 Ruby 中的整数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有以下代码:
x = 0.8
y = 1.0
检查 y
是否等同于 Integer
的最佳方法是什么?目前我正在做:
What's the best way of checking that y
is equivalent to an Integer
? At the moment I'm doing:
y.to_int == y
哪个有效,但我觉得应该有更好的方法.
which works, but I feel like there should be a better way.
推荐答案
你mod
将值设为 1,并检查值是否等于 0.
You mod
the value with 1, and check if the value equals 0.
if y % 1 == 0
这篇关于检查 Float 是否等效于 Ruby 中的整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文