转到lang如何检查float值是否实际上是int [重复]
问题描述:
This question already has an answer here:
func isFloatInt(floatValue float64) bool{
//What's the implementation here?
}
Test cases:
input: 1.5 output: false;
input: 1 output: true;
input:1.0 output: true;
</div>
此问题已经存在 在这里有一个答案: p>
-
如何在Go语言中测试浮点数是否为整数?
5个答案
span>
li>
ul>
div>
func isFloatInt(floatValue float64)bool { //这里的实现是什么? } code> pre>
测试用例: b> input:1.5输出:false;
input:1输出:true;
input:1.0输出:true;
p> div>
答
I just checked on the playground, this does the right thing with NaN as well.
func isIntegral(val float64) bool {
return val == float64(int(val))
}
答
You could achieve it by doing a modulus
func isFloatInt(floatValue float64) bool {
return math.Mod(floatValue, 1.0) == 0
}