当前位置:网站首页>BigDecimal use
BigDecimal use
2022-07-26 02:12:00 【Array_ new】
Preface :
When using single and double precision float and double There are errors in the face of tens of millions of decimal degrees in the operation process of , It's not friendly for our project , Regarding this BigDecimal Can solve the problem well BigDecimal Class provides arithmetic , Zoom operation , Round off , Compare , Hash and format conversion operations .
BigDecimal Use :
ONE // value 1, Scale is 0.
ROUND_CEILING // Round to positive infinity
ROUND_DOWN // Round to zero
ROUND_FLOOR // Round to negative infinity
ROUND_HALF_DOWN // towards ( distance ) The nearest side is rounded , Except on both sides ( Distance of ) It's equal , If so , Round down , for example 1.55 Leave one decimal place and the result is 1.5
ROUND_HALF_EVEN // towards ( distance ) The nearest side is rounded , Except on both sides ( Distance of ) It's equal , If so , If the number of reserved digits is odd , Use ROUND_HALF_UP, If it's even , Use ROUND_HALF_DOWN
ROUND_HALF_UP // towards ( distance ) The nearest side is rounded , Except on both sides ( Distance of ) It's equal , If so , Round up , 1.55 Leave one decimal place and the result is 1.6
ROUND_UNNECESSARY // The results are accurate , There's no rounding pattern
ROUND_UP // To stay away from 0 Rounding in the direction of
Because we use two decimal places , You need to set the last two digits
BigDecimal(double val, MathContext mc)
// take double Convert to BigDecimal , Rounding according to context settings .
BigDecimal(double val) // take double Convert to BigDecimal , This is a double The exact decimal representation of the binary floating-point value of .
Converted in this way BigDecimal object , The final value obtained is different from the value we want, such as :
BigDecimal d1 = new BigDecimal(0.6);
The output value is :
0.59999999999999997779553950749686919152736663818359375
And for String The type and int There is no such problem for the numerical value of type , So try to use String The type and int Type conversion value
In the use of BigDecimal When adding, it appears as 0 The situation of
BigDecimal sum= new BigDecimal("0");
sum.add(entity.getB_points_available());
This is because in the bigdecimal During the operation, the result will not be given to the object of the original operation, but to the newly generated object , So use another object to receive values
sum=sum.add(entity.getB_points_available());
边栏推荐
- I.MX6UL核心模块使用连载-WIFI测试 (八)
- SQL手工盲注、报错注入
- 【红队】ATT&CK - 利用BITS服务实现持久化
- vite 本地运行首次进入页面加载慢问题
- A pluggable am335x industrial control module onboard WiFi module
- Kaggle registration method to solve the problem of man-machine verification
- js给页面添加随机像素噪声背景
- 图解B+树的插入过程
- Make and makefile summary I
- i.MX6ULL SNVS电源域GPIO状态保持验证
猜你喜欢

i. Mx6ull snvs power domain GPIO status hold verification

Be careful about bitmap, the "memory Assassin"~

BGP knowledge points summary

SQL manual blind injection and error reporting injection
![[paper reading] coat: CO scale conv attentional image transformers](/img/d4/13ac8cdce07999d4fd51aa23173190.png)
[paper reading] coat: CO scale conv attentional image transformers

Worthington papain - production of glycopeptides from purified proteoglycans (attached Literature)

【2021】【论文笔记】6G技术愿景——OTFS调制技术

1. Mx6ul core module uses serial can and buzzer test (XI)

增删改查业务的快速上手

我来图书馆小程序签到流程分析
随机推荐
2022-07-17
Dest0g3 520 orientation (under update)
登堂入室soc之编程基础环境变量设置
opengauss如何手工安装(非OM方式)
CD from grabbing the track to building a streaming media server -- a case study of "moon in the hometown of sleep"
Error reporting caused by local warehouse
i.MX6ULL SNVS电源域GPIO状态保持验证
1. Mx6ul core module use serial -rs485 test (x)
What is the difference between for... In... And for... Of
Bitmap这个“内存刺客”你要小心~
【LeetCode】32、 最长有效括号
Sqlyog data import and export graphic tutorial
Activiti workflow gateway
一款可插拔的AM335X工控模块板载wifi模块
[C language brush leetcode] 443. Compressed string (m)
i. Mx6ull snvs power domain GPIO status hold verification
国标GB28181协议视频平台EasyGBS消息弹框模式优化
【2019】【论文笔记】基于超材料可调谐THz宽频吸收——
SQLyog数据导入导出图文教程
重发布基础与配置