Java基础之数学计算类

数学计算类

Math

用法

主要进行数学计算操作

  • 构造方法私有化,所有方法都是静态方法
  • 提供的都是基础数学公式,实际需求需要整合操作
Math.cos(0));		// 0度的余弦值
Math.atan(1)); 		// 1的反正切值
Math.PI;			// 圆周率

(int)(a + Math.random()*(b - a + 1))		// 指定范围 a ~ b 随机数公式

public class Test {
    private Test(){}
    /**
     * 实现数据的四舍五入操作
     * @param num 要进行四舍五入操作的数字
     * @param scale 四舍五入保留的小数位数
     * @return 四舍五入处理后的结果
     */
    public static double round(double num, int scale) {
        return Math.round(num * Math.pow(10, scale)) / Math.pow(10, scale);
    }
    public static void main(String[] args) {
        System.out.println(Test.round(19.86273 ,2));
    }
}
方法列表
方法描述
xxxValue():无参数将Number对象转换为xxx类型的数据并输出
compareTo():参数类型:Byte、Double、Integer、Float、Long、Short将number对象与参数比较(两个相同类型数据);相等返回0,小于参数返回-1,大于参数返回1
equals():参数可为任何对象判断number对象是否与参数相等;相等返回true,否则为false
valueOf():如果是双参数,第二个为第一个参数的基数表现形式(进制)返回一个Number对象指定的内置数据类型
toString()以字符串形式返回值
parseInt():如果是双参数,第二个为第一个参数的基数表现形式(进制)将字符串解析为int类型
abs() :参数类型:int, float, long, double, short, byte返回参数的绝对值
ceil()返回大于等于(<=)给定参数的double类型最小整数
floor()返回小于等于(<=)给定参数的double类型最大整数
rint()返回与参数最接近的double类型整数
round()四舍五入(Math.floor(x+0.5))即原来数字+0.5向下取整
min()返回两个参数中的最小值
max()返回两个参数中的最大值
exp()返回自然数底数e的参数次方
log()返回参数的自然数底数的对数值
pow()返回第一个参数的第二个参数次方
sqrt()求参数的算数平方根
sin()求指定double类型参数的正弦值
cos()求指定double类型参数的余弦值
tan()求指定double类型参数的正切值
asin()求指定double类型参数的反正弦值。
acos()求指定double类型参数的反余弦值。
atan()求指定double类型参数的反正切值。
atan2()将笛卡尔坐标转换为极坐标,并返回极坐标角度值
toDegrees()将参数转换为角度
random()返回一个随机数;范围0.0 <= R <1.0
近似值
  • floor、round和ceil取值
参数Math.floorMath.ceilMath.round
1.4121
1.5122
1.6122
-1.4-2-1-1
-1.5-2-1-1
-1.6-2-1-2

Random

  • 生成随机数

    • public int nextInt(int bound){}
      • 随机生成不大于bound的随机正整数
      • 范围:[0, bound)
  • Math.random() 也可以生成随机数

    • 范围 [0, 1);取不到1
    • 返回指定范围a~b随机数公式
      • (int)(a + Math.random()*(b - a + 1))
import java.util.Random;
/**
生成随机数
*/
public class Test{
    public static void main(String[] args) {
        Random random = new Random();
        int a =  random.nextInt(100);		// 数字在 0 ~ 100 之间,但取不到 100
        System.out.println(a);
    }
}
运行结果:
19											//0 <= a < 100

BigDecimal、BigInteger

  • 大数字计算类,实现海量数字的计算

    • 继承Number
  • BigInteger 类构造

    • public BigInteger(String val);
    • 不能接收 int
  • BigDecimal 类构造

    • public BigDecimal(String val);
    • public BigDecimal(doublee val);
    • public BigDecimal(BigInteger val);
  • BigDecimalMath

    • Math 的处理使用的都是基本数据类型,性能高于大数字处理类
  • 计算时没有超过基本数据类型包含的位数强烈不建议使用大数字处理类方法

    • 使用需要考虑性能问题,消耗很大
  • BigIntegerBigDecimal 方法类似

    • 但在 BigDecimal 中有数据进位问题
    /**
     * BigInteger的基础运算操作
     */
    public class Test {
        public static void main(String[] args) throws Exception {
            BigInteger bigA = new BigInteger("234134234234234234");
            BigInteger bigB = new BigInteger("23423423");
            System.out.println("加法操作:" + bigA.add(bigB));
            System.out.println("减法操作:" + bigA.subtract(bigB));
            System.out.println("乘法操作:" + bigA.multiply(bigB));
            System.out.println("除法操作:" + bigA.divide(bigB));
            BigInteger result[] = bigA.divideAndRemainder(bigB);			//求余
            /*
            数组第一个元素为商,第二个为余数
    		Public BigInteger[] divideAndRemainder(BigInteger val){}
            */
            System.out.println("商:" + result[0] + "、余数:" + result[1]);
        }
    }
    
  • BigDecima除法操作

    // BigDecimal 除法
    public BigDecimal divide(BigDecimal divisor,int scale,RoundingMode roundingMode);
    
    import java.math.BigDecimal;
    import java.math.RoundingMode;
    public class Test {
        private Test() { }
        /**
         * 实现数据的四舍五入操作
         * @param num   要进行四舍五入操作的数字
         * @param scale 四舍五入保留的小数位数
         * @return 四舍五入处理后的结果
         */
        public static double round(double num, int scale) {
            return new BigDecimal(num).divide(new BigDecimal(1.0), scale, RoundingMode.HALF_UP).doubleValue();
        }
        public static void main(String[] args) throws Exception {
            BigDecimal bigA = new BigDecimal("32890234890");
            BigDecimal bigB = new BigDecimal("1892039");
            // 加法计算: 32892126929
            System.out.println("加法计算: " + bigA.add(bigB));		 
            BigDecimal result[] = bigA.divideAndRemainder(bigB);	
            // 除法计算: 商 = 17383	余数 = 920953
            System.out.println("除法计算: 商 = " + result[0] + "\t余数 = " + result[1]);
            // 四舍五入: 19.64
            System.out.println("四舍五入: " + Test.round(19.6352, 2));  		
        }
    }
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值