首页 > 其他分享 >bigdecimal保留两位小数

bigdecimal保留两位小数

时间:2023-12-04 16:31:31浏览次数:29  
标签:舍入 两位 bigdecimal Rounding round mode HALF ROUND 小数

1   public class test1_format {

2   public static void main(String[] args) {

3   BigDecimal decimal = new BigDecimal("1.12345");

4   System.out.println(decimal);

5   BigDecimal setScale = decimal.setScale(4,BigDecimal.ROUND_HALF_DOWN);

6   System.out.println(setScale);

7

8   BigDecimal setScale1 = decimal.setScale(4,BigDecimal.ROUND_HALF_UP);

9   System.out.println(setScale1);

10  }

11  }

参数定义

ROUND_CEILING

Rounding mode to round towards positive infinity.

向正无穷方向舍入

ROUND_DOWN

Rounding mode to round towards zero.

向零方向舍入

ROUND_FLOOR

Rounding mode to round towards negative infinity.

向负无穷方向舍入

ROUND_HALF_DOWN

Rounding mode to round towards “nearest neighbor” unless both neighbors are equidistant, in which case round down.

向(距离)最近的一边舍入,除非两边(的距离)是相等,如果是这样,向下舍入, 例如1.55 保留一位小数结果为1.5

ROUND_HALF_EVEN

Rounding mode to round towards the “nearest neighbor” unless both neighbors are equidistant, in which case, round towards the even neighbor.

向(距离)最近的一边舍入,除非两边(的距离)是相等,如果是这样,如果保留位数是奇数,使用ROUND_HALF_UP ,如果是偶数,使用ROUND_HALF_DOWN

ROUND_HALF_UP

Rounding mode to round towards “nearest neighbor” unless both neighbors are equidistant, in which case round up.

向(距离)最近的一边舍入,除非两边(的距离)是相等,如果是这样,向上舍入, 1.55保留一位小数结果为1.6

ROUND_UNNECESSARY

Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.

计算结果是精确的,不需要舍入模式

ROUND_UP

Rounding mode to round away from zero.

向远离0的方向舍入

标签:舍入,两位,bigdecimal,Rounding,round,mode,HALF,ROUND,小数
From: https://blog.51cto.com/u_16207938/8680038

相关文章

  • Java之API详解之BigDecimal类的详细解析
     7BigDecimal类7.1引入首先我们来分析一下如下程序的执行结果:publicclassBigDecimalDemo01{publicstaticvoidmain(String[]args){System.out.println(0.09+0.01);}}这段代码比较简单,就是计算0.09和0.01之和,并且将其结果在控制台进行输出。那么......
  • 【JavaSE】一些常见API(Object、Objects、Math、System、BigDecimal、包装类、Arrays)
    Object类Object类介绍toString方法直接println(对象名),默认会自动调用(对象名.toString),而.toString默认是返回地址信息->全类名(包名+类名)@地址的十六进制哈希值,因此如果println(对象名)控制台没有输出地址值,说明该类一定重写了Object类的toString方法,比如String类和Arr......
  • 子查询、Concat 字符拼接 ,Cast截取小数位 函数使用
    selectqh.CaseId,(selectsh.CaseIdfromServiceQuot.dbo.Headershwhereqh.QutoNo=sh.HeaderNo),qh.ApplierDate,qh.BU,qh.Site,qh.HeaderNo,qh.Currency(selectsh.CustomerfromServiceQuot.dbo.Headershwhereqh.QutoNo=sh.HeaderNo),qh.PN......
  • python保留小数点后几位的方法
    一、保留小数点后n位方法一:使用字符串格式化注意:使用字符串格式化后的是字符串格式a=12.3456print("%.3f"%a)#保留小数点后三位print("%.2f"%a)#保留小数点后两位输出12.34612.35方法二:使用round内置函数注意:使用round后的是浮点数格式a=12.3456a1=round(a......
  • AM62x GPMC并口如何实现“小数据-低时延,大数据-高带宽”—ARM+FPGA低成本通信方案
    GPMC并口简介GPMC(GeneralPurposeMemoryController)是TI处理器特有的通用存储器控制器接口,支持8/16bit数据位宽,支持128MB访问空间,最高时钟速率133MHz。GPMC是AM62x、AM64x、AM437x、AM335x、AM57x等处理器专用于与外部存储器设备的接口,如:(1)FPGA器件(2)ADC器件(3)SRAM内存(4)N......
  • bigdecimal转integer
    将BigDecimal转换为Integer,可以使用intValue()方法。这个方法将BigDecimal对象转换为一个整数类型的值(即int类型),然后将其自动装箱为Integer类型。以下是示例代码:importjava.math.BigDecimal;publicclassBigDecimalToIntegerExample{publicstaticvoidmain......
  • BigDecimal加减乘(cheng)除BigDecimal[]数组比较大小
     文章目录前言一、Bigdecimal是什么?二、使用步骤总结 前言提示:BigDecimal在使用的过程中尽量采用String类型的参数进行转换尽量避免不使用字符串的数字对小数点进行数据格式的转换,避免出现精准问题。加法:add()函数减法:subtract()函数乘法:multiply()函数除法:divide()函数绝对值......
  • python中怎么识别判断是否是小数?
    defis_float(str):ifstr.count('.')==1:#小数有且仅有一个小数点left=str.split('.')[0]#小数点左边(整数位,可为正或负)right=str.split('.')[1]#小数点右边(小数位,一定为正)lright=''#取整数位的绝对值(排除掉负号)ifstr......
  • C语言基本功: float格式输入与输出,6位有效数字,6位小数
    可以看这个题目,要求保留6位有效数字(也就说说6个数字位有效,而不是保留6位小数)洛谷地址:https://www.luogu.com.cn/problem/P5714传送门:https://juejin.cn/s/c%E8%AF%AD%E8%A8%80%E4%BF%9D%E7%95%99%E5%85%AD%E4%BD%8D%E6%9C%89%E6%95%88%E6%95%B0%E5%AD%97  常规的控制输出......
  • input只能输入数字和一个小数点
    参考<el-inputclass="pro_input"v-model="formData.astrict"type="number"step="1"min="0"max="10000"size="mini"@keyup.native="proving1"></el-input>methods:{......