当前位置:网站首页>BigDecimal 简介,常用方法
BigDecimal 简介,常用方法
2022-07-31 13:53:00 【我的安卓之路】
常用构造函数
BigDecimal(int)
创建一个具有参数所指定整数值的对象
BigDecimal(double)
创建一个具有参数所指定双精度值的对象
BigDecimal(long)
创建一个具有参数所指定长整数值的对象
BigDecimal(String)
创建一个具有参数所指定以字符串表示的数值的对象
推荐使用BigDecimal(String)创建,原因double类型无法准确转化为指定数字。
常用方法
add(BigDecimal) 值相加
subtract(BigDecimal) 值相减
multiply(BigDecimal) 值相乘
divide(BigDecimal) 值相除
遇到除不尽出现无限不循环小数会报错,推荐使用时设置精度
bigDecimal.divide(BigDecimal,2,BigDecimal.ROUND_HALF_UP) 保留2位小数四舍五入toString()
将BigDecimal对象中的值转换成字符串
doubleValue()
将BigDecimal对象中的值转换成双精度数
floatValue()
将BigDecimal对象中的值转换成单精度数
longValue()
将BigDecimal对象中的值转换成长整数
intValue()
将BigDecimal对象中的值转换成整数
toPlainString()
将BigDecimal对象中的值转换成不带指数的字符串
保留指定位数小数
方法一:
进行除法时
bigDecimal.divide(BigDecimal,2) 保留2位小数
bigDecimal.divide(BigDecimal,2,BigDecimal.ROUND_UP) 保留2位小数向上取整
bigDecimal.divide(BigDecimal,2,BigDecimal.ROUND_DOWN) 保留2位小数向下取整
bigDecimal.divide(BigDecimal,2,BigDecimal.ROUND_HALF_UP) 保留2位小数四舍五入
方式二:
bigDecimal.setScale(位数,舍入模式);
格式化数值
NumberFormat currency = NumberFormat.getCurrencyInstance(); //货币格式化
NumberFormat percent = NumberFormat.getPercentInstance(); //百分比格式化
percent.setMaximumFractionDigits(3); //百分比小数点最多3位
BigDecimal loanAmount = new BigDecimal("12000.48"); //金额
BigDecimal interestRate = new BigDecimal("0.006"); //百分比
String loanAmountStr = currency.format(loanAmount);//金额进行格式化 str = 12,000.48
String interestRateStr = percent.format(interestRate);//百分比式化 str = 0.6%
边栏推荐
- Spark学习:为Spark Sql添加自定义优化规则
- Text similarity calculation (Chinese and English) detailed explanation of actual combat
- Open Inventor 10.12 Major Improvements - Harmony Edition
- hyperf的启动源码分析(二)——请求如何到达控制器
- DELL SC compellent 康贝存储系统怎么抓取配置信息
- VU 非父子组件通信
- 小试牛刀:Go 反射帮我把 Excel 转成 Struct
- Open Inventor 10.12 重大改进--和谐版
- 3.爬虫之Scrapy框架1安装与使用
- Golang - gin - pprof - use and safety
猜你喜欢
Spark学习:为Spark Sql添加自定义优化规则
C# control ListView usage
ADS communicate with c #
最近很火的国产接口神器Apipost体验
DELL SC compellent 康贝存储系统怎么抓取配置信息
Error: npm ERR code EPERM
3.爬虫之Scrapy框架1安装与使用
MySQL has played to such a degree, no wonder the big manufacturers are rushing to ask for it!
Miller_Rabin 米勒拉宾概率筛【模板】
Samba 远程命令执行漏洞(CVE-2017-7494)
随机推荐
Unity学习笔记 关于AVPro视频跳转功能(Seeking)的说明
Productivity Tools and Plugins
ICML2022 | 面向自监督图表示学习的全粒度自语义传播
技能大赛dhcp服务训练题
Introduction to the PartImageNet Semantic Part Segmentation dataset
MySQL【子查询】
An article makes it clear!What is the difference and connection between database and data warehouse?
C# List Usage List Introduction
leetcode:485.最大连续 1 的个数
页面整屏滚动效果
ADS communicate with c #
一篇文章讲清楚!数据库和数据仓库到底有什么区别和联系?
selenium被反爬了怎么办?
Selenium IDE for Selenium Automation Testing
技能大赛训练题:登录安全加固
我把问烂了的MySQL面试题总结了一下
小试牛刀:Go 反射帮我把 Excel 转成 Struct
ECCV 2022 | Robotic Interaction Perception and Object Manipulation
Batch大小不一定是2的n次幂!ML资深学者最新结论
Sliding window method to segment data