原文地址:https://www.cnblogs.com/liqforstudy/p/5652517.html

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 保留小数位

    public class test1_format { public static void main(String[] args) { BigDecimal decimal = new BigDec ...

  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. 用copy 还是 strong?

    NSArray与NSMutableArray用copy修饰还是strong 这个是原文  http://blog.csdn.net/winzlee/article/details/51752354 一 ...

  2. 一个web.Config或app.Config自定义段configSections的示例

    一个web.Config或app.Config自定义段configSections的示例 越来越觉得,直接用配置文件app.Config或web.Config配置应用系统的运行参数,比自己做一个xml ...

  3. thinkphp __hash__

    thinkphp会自动在模板文件的表单后加一个__hash__来验证提交,保证一定程序上的安全: 但是有时用不着,可以用: C('TOKEN_ON',false); 关闭这项

  4. C/C++返回内部静态成员的陷阱

    在我们用C/C++开发的过程中,总是有一个问题会给我们带来苦恼.这个问题就是函数内和函数外代码需要通过一块内存来交互(比如,函数返回字符串),这个问题困扰和很多开发人员.如果你的内存是在函数内栈上分配 ...

  5. oracle中exists和in的比较

    exists 是Oracle sql中的一个函数.表示是否存在符合某种条件的记录.如 select * from A,B where A.id=B.id and exists (SELECT * FR ...

  6. Python 爬虫 大量数据清洗 ---- sql语句优化

    . 问题描述 在做爬虫的时候,数据量很大,大约有五百百万条数据,假设有个字段是conmany_name(拍卖公司名称),我们现在需要从五百万条数据里面查找出来五十家拍卖公司, 并且要求字段 time( ...

  7. maven依赖包冲突解决办法

    今天在写一个demo时报了以下错误 SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding /slf4j-lo ...

  8. php分享二十二:php面向对象

    1:static访问符 在类中使用static有两种主要用途.定义静态成员和定义静态方法.静态成员只保留一个变量的值,这个值对所有实例都是有效的 类的方法是static的,他所访问的属性也必须是sta ...

  9. GCD 实现生产-消费 模式

    #import "ViewController.h" @interface ViewController (){ } @property (nonatomic,strong) di ...

  10. Atitit  404错误的排查流程总结 v3 qaf

    Atitit  404错误的排查流程总结 v3 qaf 1.1. 用了注解不生效 提示404 Not Found1 1.2. 路径不对了,开头多了个空格1 2. 500 Servlet Excepti ...