码迷,mamicode.com
首页 > 其他好文 > 详细

025_取余运算

时间:2017-05-31 19:01:06      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:math   div   mode   ret   round   turn   val   stat   ceil   

public without sharing class MathHelper {
    
    /*
        是否整除
        @param dividend 被除数
        @param divider  除数
        @return 整除返回true,不整除返回false
    */
    public static Boolean isDivisibility(Integer dividend,Integer divider) {
        //先将被除数转换成Decimal,否则先计算整数除法结果为Integer类型,然后将整数转换成Decimal
        Decimal resultDecimal = Decimal.valueOf(dividend) / divider;
        //获取结果上线
        Integer resultSeiling = Integer.valueOf(resultDecimal.round(System.RoundingMode.CEILING));
        Integer resultDown = Integer.valueOf(resultDecimal.round(System.RoundingMode.DOWN));
        
        return resultSeiling == resultDown;
    }
    
    /*
        获取余数
        @param dividend 被除数
        @param divider  除数
        @return 返回余数
    */
    public static Integer getRemainder(Integer dividend,Integer divider) {
        Decimal resultDecimal = Decimal.valueOf(dividend) / divider;
        Integer resultDown = Integer.valueOf(resultDecimal.round(System.RoundingMode.DOWN));
        return dividend - resultDown * divider;
    }
}

025_取余运算

标签:math   div   mode   ret   round   turn   val   stat   ceil   

原文地址:http://www.cnblogs.com/bandariFang/p/6925086.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!