当前位置:网站首页>两个BigDecimal数据类型比较、加减乘除、格式化
两个BigDecimal数据类型比较、加减乘除、格式化
2022-06-10 11:34:00 【yechaoa】
一般情况下,string类型比较用equals,int用=
而BigDecimal需要用compareTo
if(goodsData.unitPrice.compareTo(new BigDecimal("0.00"))==0){
etGoodsPrice.setText("");
}else{
etGoodsPrice.setText(String.valueOf(goodsData.unitPrice));
}上面的代码就是两个BigDecimal数据类型的值进行比较
new BigDecimal("0.00")是确定数据格式,两位小数
==0,文档中也有介绍,0表示相等,-1表示小于,1表示大于
加减乘除:
BigDecimal b=new BigDecimal(100);
假设一个int值:int c=5; 如果是BigDecimal类型的就不用转换
1.加
b.add(BigDecimal.valueOf(c));
2.减
b.subtract(BigDecimal.valueOf(c));
3.乘
b.multiply(BigDecimal.valueOf(c));
4.除
b.divide(BigDecimal.valueOf(c));
格式化:
.setScale(2) 保留两位小数,默认四舍五入, 1.235》1.24
.setScale(2,BigDecimal.ROUND_DOWN) 直接删除两位小数后面的小数, 1.235》1.23
.setScale(2,BigDecimal.ROUND_HALF_UP) 四舍五入,向上取, 1.235》1.24
.setScale(2,BigDecimal.ROUND_HALF_DOWN) 四舍五入,向下取, 1.235》1.23
边栏推荐
- Nucleic acid detection robot
- Pytorch tensor高階操作
- [extensive reading of papers] distilling the knowledge in a neural network
- Detailed explanation of redis
- 远程解决win10上keyboard和chrome不work的两例问题(by quqi99)
- 为你推荐一款高效的IO组件——okio
- “职” 为等你!| 图书策划编辑(会议论文集方向)
- "Duty" is waiting for you| Book planning and editing (Conference Proceedings)
- 纪念正月十六工作室总访问量突破百万
- 期末考试——编译原理
猜你喜欢

【Question】rxjs/operator takeWhile vs takeUntil

线性代数的本质4 矩阵乘法与线性复合变换

Source of concurrent bugs (I) - visibility

In commemoration of the 16th day of the first month, the total number of visits to the studio exceeded one million

剑指位运算

【管理知多少】独立冲突之外,你做不到

《浅谈数组》

【万人独木桥】那个夏天—后高考生活该如何安排?

期末考试——编译原理

Flutter socketio example
随机推荐
87. (leaflet house) leaflet military plotting - straight arrow modification
有在网上股票开户的吗?股票网上开户安全吗?
北大、微软|关于使语言模型更好的推理的进展
爱可可AI前沿推介(6.10)
CLIP使用
Transfomer各组件与Pytorch
数据在内存中的存储方式
Practice of Flink CDC in Dajian cloud warehouse
Practice of Flink CDC + Hudi massive data entering the lake in SF
十三、系统调用和Shell(freestanding shell, 终端和 job control)
软件测试基础
[WIP] Openstack Masakari (by quqi99)
10款值得你去选择的AirPods Pro竞争产品
LVS+Keepalived高可用群集
SqlSessionFactory和SqlSession详解
La poignée d'enseignement de la station B vous apprend à utiliser le masque yolov5 pour tester les éléments de l'enregistrement le plus complet (apprentissage profond / détection d'objets / pythorch)
2022年Z-Wave生态系统状态报告
Weekly 5.28
时间轴、物流信息。你根本不需要StepView
Do you have an online stock account? Is it safe to open an account online?