当前位置: 首页 > news >正文

BigInteger和BigDecimal类

一、应用场景

1. BigInteger 类

目前,我们学过最大的是long类型,但是,在实际开发时候,很有可能遇见超出long类型范围的数,我们就需要用BigInteger类;

① add    加

② subtract    减

③ multiply   乘

④ divide   除

public class BigInteger_ {public static void main(String[] args) {BigInteger bigInteger = new BigInteger("299999999999999999999999999999999");System.out.println(bigInteger);
//        在对BigInteger类型的数据进行 加减乘除 时,不能直接+ - * /
//        要使用相应的方法BigInteger bigInteger1 = new BigInteger("100");
//        1.加BigInteger add = bigInteger.add(bigInteger1);System.out.println(add);
//        2.减BigInteger subtract = bigInteger.subtract(bigInteger1);System.out.println(subtract);
//        3.乘BigInteger multiply = bigInteger.multiply(bigInteger1);System.out.println(multiply);
//        4.除BigInteger divide = bigInteger.divide(bigInteger1);System.out.println(divide);}
}

 

 2. BigDecimal 类

目前,我们学过精度最高的是double类型,但是,在实际开发时候,很有可能需要精度更高的类型,我们就需要用BigDecimal类; 

public class BigDecimal_ {public static void main(String[] args) {BigDecimal bigDecimal = new BigDecimal("2.99999999999999999999999999999999");System.out.println(bigDecimal);
//        在对BigDecimal类型的数据进行 加减乘除 时,不能直接+ - * /
//        要使用相应的方法BigDecimal bigDecimal1 = new BigDecimal("1.63");
//        1.加BigDecimal add = bigDecimal.add(bigDecimal1);System.out.println(add);
//        2.减BigDecimal subtract = bigDecimal.subtract(bigDecimal1);System.out.println(subtract);
//        3.乘BigDecimal multiply = bigDecimal.multiply(bigDecimal1);System.out.println(multiply);
//        4.除BigDecimal divide = bigDecimal.divide(bigDecimal1);//可能会抛出异常
//        在调用divide 方法时,指定精度即可
//        如果有无限循环小数,就会保留  分子  的精度BigDecimal divide1 = bigDecimal.divide(bigDecimal1,BigDecimal.ROUND_CEILING);System.out.println(divide1);}
}

http://www.lryc.cn/news/408898.html

相关文章:

  • 2024最新Uniapp的H5网页版添加谷歌授权验证
  • 学习java第一百四十四天
  • Meta 发布 Llama3.1,一站教你如何推理、微调、部署大模型
  • XSSFWorkbook 和 SXSSFWorkbook 的区别
  • 会议主题:NICE Seminar|神经组合优化方法的大规模泛化研究(南方科技大学王振坤副研究员)
  • 昇思25天学习打卡营第22天|CycleGAN图像风格迁移互换
  • 《Java初阶数据结构》----6.<优先级队列之PriorityQueue底层:堆>
  • Matrix Equation(高斯线性异或消元+bitset优化)
  • 【一图学技术】2.API测试9种方法图解
  • 力扣刷题----42. 接雨水
  • 【论文精读】 | 基于图表示的视频抑郁症识别的两阶段时间建模框架
  • 采集PCM,将base64片段转换为wav音频文件
  • eclipse ui bug
  • 前端获取blob文件格式的两种格式
  • 向日葵RCE复现(CNVD-2022-10270/CNVD-2022-03672)
  • Postman中的负载均衡测试:确保API的高可用性
  • anaconda+tensorflow+keras+jupyter notebook搭建过程(CPU版)
  • LitCTF2024赛后web复现
  • Elasticsearch:跨集群使用 ES|QL
  • 学习笔记4:docker和k8s选择简述
  • 关于锁策略
  • 昇思25天学习打卡营第3天|基础知识-数据集Dataset
  • C++11新特性——智能指针——参考bibi《 原子之音》的视频以及ChatGpt
  • “微软蓝屏”全球宕机,敲响基础软件自主可控警钟
  • 【Linux C | 网络编程】进程间传递文件描述符socketpair、sendmsg、recvmsg详解
  • 高并发内存池(六)Page Cache回收功能的实现
  • 浅析JWT原理及牛客出现过的相关面试题
  • Spring AI (五) Message 消息
  • 【windows Docker desktop】在git bash中报错 docker: command not found 解决办法
  • 02.FreeRTOS的移植