美文网首页
Java 中判断双Double数是否整除

Java 中判断双Double数是否整除

作者: 丶晓虎 | 来源:发表于2019-05-15 17:57 被阅读0次

在java中,对于大于1的数A和B且A大于B的情况下,直接使用A%B==0可以判断A是否是B的倍数,即A可以被B整除,但是如果是小数的情况下,则方法可能不是100%有效,就拿0.08来说,经过几次测试,0.16,0.32都没问题,唯独0.24取余竟然是0.079999999,不论在Java还是Js都是一个毛病,索性换算成整数来判断。
粗略的代码,记录下,可能有改进的地方
Java代码

/**
     * 判断是否倍数或整除
     * @param x 大值
     * @param y 小值
     * @return 结果
     * @date 2019年5月15日
     */
    public static boolean isPositiveIntegerTimes(Double x, Double y) {
            Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
            if (x < y || x == y) {
                return false;
            }
            Integer newX = 1;
            Integer newY = 1;
            if (x <= 1 && y < 1) {
                int t1 = x.toString().split("\\.")[1].length();
                int t2 = y.toString().split("\\.")[1].length();
                int pInt = 1;
                if (t1 > t2) {
                    pInt = t1;
                }
                else if (t1 < t2) {
                    pInt = t2;
                }
                else {
                    pInt = t1;
                }
                Double powNum = Math.pow(10, pInt);
                Double xNum = powNum * x;
                Double yNum = powNum * y;
                // 同乘相同倍数
                newX = xNum.intValue();
                newY = yNum.intValue();
            }
            else if (x >= 1 && y < 1) {
                if (x % 1 == 0) {
                    newX = x.intValue();
                    System.err.println("目标数检测到整数:" + x);
                }
                if (y % 1 == 0) {
                    newY = y.intValue();
                    System.err.println("来源数检测到整数:" + y);
                }
                int t1 = x.toString().split("\\.")[1].length();
                int t2 = y.toString().split("\\.")[1].length();
                int pInt = 1;
                if (t1 > t2) {
                    pInt = t1;
                }
                else if (t1 < t2) {
                    pInt = t2;
                }
                else {
                    pInt = t1;
                }
                Double powNum = Math.pow(10, pInt);
                Double xNum = powNum * x;
                Double yNum = powNum * y;
                // 同乘相同倍数
                newX = xNum.intValue();
                newY = yNum.intValue();
            }
            else if (x >= 1 && y >= 1) {
                if (pattern.matcher(x.toString()).matches()) {
                    newX = x.intValue();
                    System.err.println("目标数检测到整数:" + x);
                }
                if (pattern.matcher(y.toString()).matches()) {
                    newY = y.intValue();
                    System.err.println("来源数检测到整数:" + y);
                }
                int t1 = x.toString().split("\\.")[1].length();
                int t2 = y.toString().split("\\.")[1].length();
                int pInt = 1;
                if (t1 > t2) {
                    pInt = t1;
                }
                else if (t1 < t2) {
                    pInt = t2;
                }
                else {
                    pInt = t1;
                }
                Double powNum = Math.pow(10, pInt);
                Double xNum = powNum * x;
                Double yNum = powNum * y;
                // 同乘相同倍数
                newX = xNum.intValue();
                newY = yNum.intValue();
            }
            if (newX % 1 == 0 && newY % 1 == 0) {
                if (newX % newY == 0) {
                    return true;
                }
                else {
                    return false;
                }

            }
            else {
                return false;
            }
    }

Js代码

function isPositiveIntegerTimes(x,y){
    if (x < y || x == y) {
        return false;
    }
    var newX = 0;
    var newY = 0;
    if (x < 1 && y < 1) {
        var t1 = x.toString().split(".")[1].length;
        var t2 = y.toString().split(".")[1].length;
        var pInt = 1;
        if (t1 > t2) {
            pInt = t1;
        }
        else if (t1 < t2) {
            pInt = t2;
        }
        else {
            pInt = t1;
        }
        var powNum = Math.pow(10, pInt);
        var xNum = powNum * x;
        var yNum = powNum * y;
        // 同乘相同倍数
        newX = Math.ceil(xNum);
        newY = Math.ceil(yNum);
    }
    else if (x >= 1 && y >= 1) {
        newX = Math.ceil(x);
        newY = Math.ceil(y);
    }
    if (newX % newY == 0) {
        return true;
    }
    else {
        return false;
    }
}

相关文章

  • Java 中判断双Double数是否整除

    在java中,对于大于1的数A和B且A大于B的情况下,直接使用A%B==0可以判断A是否是B的倍数,即A可以被B整...

  • Day3 作业

    写出判断一个数是否能同时被3和7整除的条件语句 写出判断一个数是否能够被3或者7整除,但是不能同时被3或者7整除 ...

  • Day3-运算符&变量&作业

    作业 写出判断一个数是否能同时被3和7整除的条件语句 写出判断一个数是否能够被3或者7整除,但是不能同时被3或者7...

  • NSScaner类的用法 判断输入的字符串类型

    判断是否为整形 判断是否为浮点型 判断是否为double双精度型 以字符串“132 panda lxl of ap...

  • 简单算法题

    斐波那契数列 判断一个数是否是质数(只能被1和本身整除) 判断是否是丑数丑数就是只包含质因数 2, 3, 5 的正...

  • java中的NaN和Infinity

    今天,我朋友给我问了一个问题:怎么快速判断一个 double 数是否在另外两个 double 数之间?(说这是个面...

  • 04.打印100以内的质数?

    质数:在大于1的自然数中,除了1和它本身以外不再被其他数整除。判断条件:只能被整除2次的数,就是了

  • 作业002:变量与运算符部分

    1. 写出判断一个数是否能同时被3和7整除的条件语句, 并且打印对应的结果。  eg: 2. 写出判断一个数是否能...

  • 0014-判断闰年

    问题描述 判断某年是否是闰年。公历纪年法中,能被 4 整除的大多是闰年,但能被 100 整除而不能被 400 整除...

  • Numpy 求100以内质数和

    一百以内质数之和 判断是否为质数 判断一个整数是否为质数比较简单,即除了自身和1以外不可被别的数整除。不过根据数学...

网友评论

      本文标题:Java 中判断双Double数是否整除

      本文链接:https://www.haomeiwen.com/subject/ppsmaqtx.html