如何确定双精度数是否为整数? [英] How can you determine whether a double is an integer?

查看:135
本文介绍了如何确定双精度数是否为整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Swift 3中是否仍然可以确定双精度值是否具有小数位?在我的程序中,如果它是整数值,我只想对这个double进行计算。如何检查小数点后是否有非零数字?

Is there anyway in Swift 3 to determine whether a double value has decimal places or not? In my program, I only want to perform a calculation to this double if it is an integer value. How can I check to see if there's non-zero numbers after the decimal point?

例如:

let double dx = 1.0 // This would return true
let double dy = 1.5 // This would return false

任何帮助都将受到赞赏!谢谢,

Any and all help is appreciated! Thanks,

马修

推荐答案

extension Double {
    var isInt: Bool {
        let intValue = Int(self)
        return  Double(intValue) == self
    }
}

**当值超出Int范围时不起作用。

**Not working when value out of Int range.

这篇关于如何确定双精度数是否为整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆