Go lang 如何检查浮点值是否实际上是 int


func isFloatInt(floatValue float64) bool{
//What's the implementation here?
}

测试用例:
输入:1.5 输出:假;
输入:1 输出:真;
输入:1.0 输出:真;

我刚刚在操场上检查过,这对 NaN 也有正确的作用。

func isIntegral(val float64) bool {
    return val == float64(int(val))
}

你可以通过做一个模来实现它

func isFloatInt(floatValue float64) bool {
    return math.Mod(floatValue, 1.0) == 0
}

相关内容

最新更新