JavaSE学习笔记 - 数字类、随机数

BigInteger

import java.math.BigInteger;

public class Main {
    public static void main(String[] args) {
    	//加
        BigInteger ans = BigInteger.ONE;
        for (int i = 0; i < 1000; i++) {
            ans = ans.add(BigInteger.valueOf(i));
        }
        System.out.println(ans);
        //减
        ans = BigInteger.ZERO;
        for (int i = 0; i < 1000; i++) {
            ans = ans.subtract(BigInteger.valueOf(i));
        }
        System.out.println(ans);
        //乘
        ans = BigInteger.ONE;
        for (int i = 1; i < 1000; i++) {
            ans = ans.multiply(BigInteger.valueOf(i));
        }
        System.out.println(ans);
        System.out.println(ans.gcd(BigInteger.ONE)); //gcd
        System.out.println(ans.mod(BigInteger.valueOf(10))); //取模
        System.out.println(ans.compareTo(BigInteger.ONE)); //比较
        ans = BigInteger.ONE;
        //除
        for (int i = 1; i < 1000; i++) {
            ans = ans.divide(BigInteger.valueOf(i));
        }
        System.out.println(ans);
    }
}

DecimalFormat

public class Main {
    public static void main(String[] args) {
        DecimalFormat df = new DecimalFormat("###,###.##");
        System.out.println(df.format(333333.44444));//333,333.44
        df = new DecimalFormat("###,###.0000");
        System.out.println(df.format(11111.11));//11,111.1100
        df = new DecimalFormat("###.##");
        System.out.println(df.format(1111.115));//1111.12
    }
}

BigDecimal

public class Main {
    public static void main(String[] args) {
        BigDecimal ans = BigDecimal.ONE;
        ans = ans.add(BigDecimal.valueOf(555.55));
        System.out.println(ans);//556.55
        ans = ans.subtract(BigDecimal.valueOf(555.55));
        System.out.println(ans);//1.00
        ans = ans.multiply(BigDecimal.valueOf(555.55));
        System.out.println(ans);//555.5500
        ans = ans.divide(BigDecimal.valueOf(555.55));
        System.out.println(ans);//1.00
        //如果是无限循环小数可以选择四舍五入的方式进行取舍
        ans = ans.divide(BigDecimal.valueOf(3), BigDecimal.ROUND_HALF_UP);
        System.out.println(ans);
    }
}

Random

public class Main {
    public static void main(String[] args) {
        //如果随机种子被固定,那么每次生成随机数都是一样的数
        //Random random = new Random(0);
        Random random = new Random();
        int num = random.nextInt(); //int范围内生成一个随机数
        System.out.println(num);//-1155484576

        for (int i = 0; i < 10; i++) {
            System.out.print(random.nextInt(100) + " "); //[1, 100);
        }
        System.out.println();//48 29 47 15 53 91 61 19 54 77

        double num1 = random.nextDouble();
        System.out.println(num1); //[0, 1.0)
        for (int i = 0; i < 10; i++) {
            System.out.print(random.nextDouble() * 100 + " "); //[0, 100.0)
        }
        System.out.println();
        //45.89635388212344 31.801944345473622 92.9140933009556 98.09388893069377 1.6510586300813146 36.87360429525213 30.092352127985432 51.19119225676931 10.159602362633658 53.97847957721588
        Boolean flag = random.nextBoolean();
        System.out.println(flag);
        for (int i = 0; i < 10; i++) {
            System.out.print(random.nextBoolean() + " ");
        }
        System.out.println();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值