public class test1_format {

    public static void main(String[] args) {
BigDecimal decimal = new BigDecimal("1.12345");
System.out.println(decimal);
BigDecimal setScale = decimal.setScale(4,BigDecimal.ROUND_HALF_DOWN);
System.out.println(setScale); BigDecimal setScale1 = decimal.setScale(4,BigDecimal.ROUND_HALF_UP);
System.out.println(setScale1);
}
}

参数定义

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 保留小数位的更多相关文章

  1. [转]bigdecimal 保留小数位

    原文地址:https://www.cnblogs.com/liqforstudy/p/5652517.html public class test1_format { public static vo ...

  2. C# 当double数值较大且小数位过多时转化成字符串并保留小数位

    今天在C#中碰到了一个问题,需要将double转换成字符串显示,要求保留小数位. 在网上查询了一下相关的文章 具体如下: double temp=3.1415926; (F)Fixed point:s ...

  3. sprintf() 处理 float类型的数字,保留小数位等。

    关于 sprintf()的百科地址: http://baike.baidu.com/view/1295144.htm sprintf(szText, "%[填空字元][宽度][.精度]f&q ...

  4. 关于 BigDecimal 的小数位的入舍去操作

    BigDecimal  保留小数 的 入舍操作, 6 中 策略 : RoundingMode 里面的 枚举 和      BigDecimal  的  常量 是等价的  UP(BigDecimal.R ...

  5. BigDecimal保留小数处理

    最近在处理支付相关的需求,涉及到金额的问题,采用传统的基本数据类型处理会存在误差,因此采用BigDecimal对象进行处理. 一.构造BigDecimal对象的方式 BigDecimal(int)   ...

  6. python保留小数位

    前言 保留小数位是我们经常会碰到的问题,尤其是刷题过程中.那么在python中保留小数位的方法也非常多,但是笔者的原则就是什么简单用什么,因此这里介绍几种比较简单实用的保留小数位的方法: 方法一:fo ...

  7. Python-其他-round()保留小数位时遇到的问题

    最近有一个需求,原有整数计算,改成小数计算,保留一位小数. 于是按照需求,将数据结构由 int 改为 float ,计算时采用round()方法来保留小数位. 第一版代码如下: a = 0.10000 ...

  8. java.math.BigDecimal保留两位小数,保留小数,精确位数

    http://blog.csdn.net/yuhua3272004/article/details/3075436 使用java.math.BigDecimal工具类实现   java保留两位小数问题 ...

  9. 问题记录——BigDecimal保留两位小数及格式化成百分比

    1.函数总结 BigDecimal.setScale()方法用于格式化小数点 setScale(1)表示保留一位小数,默认用四舍五入方式 setScale(1,BigDecimal.ROUND_DOW ...

随机推荐

  1. null和undefined区别

    undefined表示不存在的状态.没有定义的变量,没有定义的对象属性,没有return的函数的返回值等等都是undefined. null表示没有对象.使用上没有差别,只是根据大众的使用习惯,场合不 ...

  2. H5离线存储

    如何使用 首先,我们建立一个html文件,类似这样: <!DOCTYPE html> <html lang="en" manifest="manifes ...

  3. angularjs不同页面间参数的传递

    1.在路由中定义要接收的参数 .state('userDetails', { url: '/userDetails?phone', //以?为标识接收参数 templateUrl: 'assets/v ...

  4. javascript中this指针

    看完此片文章豁然开朗,非常感谢.javascript技术难点(三)之this.new.apply和call详解 下面说一说自己的理解: this指针总是指向调用他的对象,其实我更愿意理解为:this指 ...

  5. 点击按钮div显示,点击div或者document,div隐藏

    $("button").click(function(event){ event.stopPropagation(); if($("div").is(':hid ...

  6. Sql Server如何新建计划任务

    打开Sql Server数据库,在管理中找到维护计划功能,右击维护计划选择维护计划向导.如图所示: 打开维护计划向导后,在第一个步骤下输入计划名称. 输入完计划名称后,点击右下角的更改按钮,这里是配置 ...

  7. hexo 本地local4000打不开解决方法

    错误:Cannot GET /spadesq.github.io/ (注:spadesq.github.io是原来放hexo文件夹的名字) 由于我后来把hexo文件夹搬迁到别处,但我发现打开本地,地址 ...

  8. occ添加新的捕捉模式

    Load (theSelection, theShape, theType, theDeflection, theDeviationAngle, isAutoTriangulation, thePri ...

  9. Redis常用命令入门5:有序集合类型

    有序集合类型 上节我们一起学习了集合类型,感受到了redis的强大.现在我们接着学Redis的最后一个类型——有序集合类型. 有序集合类型,大家从名字上应该就可以知道,实际上就是在集合类型上加了个有序 ...

  10. c#判断IP是否可以Ping通

    Ping pingSender = new Ping(); PingReply reply = pingSender.Send("127.0.0.1",120);//第一个参数为i ...