当前位置:网站首页>BigDecimal用法常用操作记录
BigDecimal用法常用操作记录
2022-08-09 09:28:00 【潇湘梦】
BigDecimal用法详解
前期简介:
Java在java.math包中提供的API类BigDecimal,用来对超过16位有效位的数进行精确的运算。双精度浮点型变量double可以处理16位有效数。在实际应用中,需要对更大或者更小的数进行运算和处理。float和double只能用来做科学计算或者是工程计算,在商业计算中要用java.math.BigDecimal。BigDecimal所创建的是对象,我们不能使用传统的+、-、*、/等算术运算符直接对其对象进行数学运算,而必须调用其相对应的方法。方法中的参数也必须是BigDecimal的对象。构造器是类的特殊方法,专门用来创建对象,特别是带有参数的对象。商业上要求结果更加精确所有BigDecimal就派上了大用场。
代码:
System.out.println(0.2 + 0.1);
System.out.println(0.3 - 0.1);
System.out.println(0.2 * 0.1);
System.out.println(0.3 / 0.1);
运行结果
这样的结果原因在于是我们的计算机是二进制的,浮点数没办法用二进制的进行精确的标识,CPU标识浮点数由两部分组成 指数,位数 这样的表示方法会失去一定的精度,有些浮点数也会产生一定的误差。
:
2.4的二进制表示并非就是精确的2.4。反而最为接近的二进制表示是 2.3999999999999999。浮点数的值实际上是由一个特定的数学公式计算得到的。
商业中对于精度要求更加高所以BigDecimal就拥有更大的展示平台。特别是在财务领域的,金融等一些。
构造器使用
BigDecimal(int) 创建一个具有参数所指定整数值的对象。
BigDecimal(double) 创建一个具有参数所指定双精度值的对象。
BigDecimal(long) 创建一个具有参数所指定长整数值的对象。
BigDecimal(String) 创建一个具有参数所指定以字符串表示的数值的对象。
在构造器中建议优先使用String的。
BigDecimal方法
add(BigDecimal) BigDecimal对象中的值相加,然后返回这个对象。
subtract(BigDecimal) BigDecimal对象中的值相减,然后返回这个对象。
multiply(BigDecimal) BigDecimal对象中的值相乘,然后返回这个对象。
divide(BigDecimal) BigDecimal对象中的值相除,然后返回这个对象。
toString() 将BigDecimal对象的数值转换成字符串。
doubleValue() 将BigDecimal对象中的值以双精度数返回。
floatValue() 将BigDecimal对象中的值以单精度数返回。
longValue() 将BigDecimal对象中的值以长整数返回。
intValue() 将BigDecimal对象中的值以整数返回。
栗子操作:
BigDecimal abs=BigDecimal.valueOf(10).divide(BigDecimal.valueOf(3),2,BigDecimal.ROUND_CEILING);
BigDecimal abs1=BigDecimal.valueOf(10).divide(BigDecimal.valueOf(3));
System.out.println(abs);
System.out.println(abs1);
divide的时候需要设置保留几位小数是四舍五入还是其他。不然就会报错
BigDecimal a = BigDecimal.valueOf(1.2222222455);
BigDecimal b = BigDecimal.valueOf(1.4444566666);
BigDecimal c = a.add(b);
System.out.println("A+B="+c);
System.out.println("四舍五入A+B="+c.setScale(2,BigDecimal.ROUND_CEILING));
System.out.println("直接去掉 A+B="+c.setScale(3,BigDecimal.ROUND_FLOOR));

BigDecimal 必须使用compareTo()方法来比较,它根据两个值的大小分别返回负数、正数和0,分别表示小于、大于和等于。
BigDecimal a = BigDecimal.valueOf(1.2222222455);
BigDecimal b = BigDecimal.valueOf(1.4444566666);
System.out.println(a.compareTo(b));
System.out.println(b.compareTo(a));

边栏推荐
- QT sets the icon of the exe executable
- The div simulates the textarea text box, the height of the input text is adaptive, and the word count and limit are implemented
- 常用命令之思科常用基础配置
- 网络安全入门基础:IP地址
- 2048小游戏成品源码
- Consolidation of Questionnaire Questions and Answers
- Do you know the basic process and use case design method of interface testing?
- 8.递归遍历和删除案例
- 本体开发日记05-努力理解SWRL(Usage Suggestions)
- A first look at the code to start, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, the first time to run the golang program EP01
猜你喜欢

手机APP测试流程规范和方法你知道多少?
web测试之功能测试常用的方法有哪几种?有什么要点要注意?

字典

2048小游戏成品源码

Sweet alert
功能自动化测试实施的原则以及方法有哪些?

A Practical Guide to Building OWL Ontologies using Protege4 and CO-ODE Tools - Version 1.3 (7.4 Annotation Properties - Annotation Properties)

Cisco common basic configuration of common commands
一个项目的整体测试流程有哪几个阶段?测试方法有哪些?

白盒测试的概念、目的是什么?及主要方法有哪些?
随机推荐
8.递归遍历和删除案例
本体开发日记05-努力理解SWRL(Usage Suggestions)
GBase数据库中,源为 oracle 报出“ORA-01000:超出打开游标最大数”
Domestic with Google earth software, see the download 19th level high-resolution satellite images so easy!
Go-指针的那些事
年薪40W测试工程师成长之路,你在哪个阶段?
游戏测试的概念是什么?测试方法和流程有哪些?
米斗APP逆向分析
5.Set接口与实现类
The div simulates the textarea text box, the height of the input text is adaptive, and the word count and limit are implemented
在anaconda环境中配置cuda和cudnn
接口性能测试方案设计方法有哪些?要怎么去写?
迭代
mysql简单安装
5. Transform Streams
1. Introduction to threads
Do you know the basic process and use case design method of interface testing?
图表示学习(Graph Representation Learning)笔记
Firebase+Facebook 授权 web 登录提示域名验证或跳转错误
软件测试面试常见问题及答案(发散思维、接口、性能、概念、)