栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java System类,大数处理方案指南

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java System类,大数处理方案指南

java System类,大数处理方案指南
  • 1.System类
  • 2.BigInteger
  • 3.BigDecimal

1.System类

该类实现了一些关于系统功能的方法:

import java.util.Arrays;


public class SystemTest {
    public static void main(String[] args) {
        int[] src = {1, 2, 3};
        int[] dest = new int[3];
        
        System.arraycopy(src, 0, dest, 0, 3);
        System.out.println(Arrays.toString(dest));
        // 返回当前时间距离1970年1月1日的毫秒数
        System.out.println(System.currentTimeMillis());
        // 调用垃圾回收机制
        System.gc();
        // 退出当前程序
        // 0:程序正常退出
        System.exit(0);
    }
}

2.BigInteger

该类实现了大整数的运算:

// BigInteger
BigInteger bigInteger = new BigInteger("11111111111111111111111");
BigInteger bigInteger1 = new BigInteger("12345678910");
// +
BigInteger addRes = bigInteger.add(bigInteger1);
System.out.println(addRes);
// -
BigInteger subRes = bigInteger.subtract(bigInteger1);
System.out.println(subRes);
// *
BigInteger mulRes = bigInteger.multiply(bigInteger1);
System.out.println(mulRes);
// /
BigInteger divRes = bigInteger.divide(bigInteger1);
System.out.println(divRes);

3.BigDecimal

该类实现了超高精度的浮点数运算:

// BigDecimal
BigDecimal bigDecimal = new BigDecimal("1.1111111111111111111999999999999");
BigDecimal bigDecimal1 = new BigDecimal("1.234567");
System.out.println(bigDecimal);
// +
BigDecimal daddRes = bigDecimal.add(bigDecimal1);
System.out.println(daddRes);
// -
BigDecimal dsubRes = bigDecimal.subtract(bigDecimal1);
System.out.println(dsubRes);
// *
BigDecimal dmulRes = bigDecimal.multiply(bigDecimal1);
System.out.println(dmulRes);
// /,注意:如果不指定精度,结果是死循环小数,会抛出一个异常
BigDecimal ddivRes = bigDecimal.divide(bigDecimal1,BigDecimal.ROUND_CEILING);
System.out.println(ddivRes);
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1020331.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号