原文地址: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. STS 设置代码注释模板

    打开Window->Preferences->Java->Code Style->Code Templates <?xml version="1.0" ...

  2. mvc 缓存页面 减轻服务器压力

    方法: using System; using System.Collections.Generic; using System.Linq; using System.Web; using Syste ...

  3. Ilist<object>转换成I<实体> 如何转换

    ml = objects.Cast<menu>().ToList(); //需要 using System.Linq;

  4. Android Notification 的声音和震动

    我们在Android系统发送一条Notification的时候,经常需要通过震动或声音来提醒用户.如何为Notification设置声音和震动了.大致思路有: - AndroidNotificatio ...

  5. Html5 canvas 绘制彩票走势图

    因须要 要实现一个类似彩票走势图的功能,初次学Html5 ,非常多地方不明白,前段时间也发帖请教过这个问题.也是没给个明白说话,在网上搜了非常多,也没有实现的样例,今天细致研究了下.发现事实上也不是非 ...

  6. Redis使用问题及知识点记录 - 待整理

    介绍 官网:https://redis.io/commands/expire spring data redis 整合redis使用方法 spring 整合api :http://docs.sprin ...

  7. hive SQL 字母大小写转换

    lower(string A) lcase(string A) 将文本字符串转换成字母全部小写形式 upper(string A) ucase(string A) 将文本字符串转换成字母全部大写形式

  8. Spring Hibernate JPA 联表查询 复杂查询(转)

    今天刷网,才发现: 1)如果想用hibernate注解,是不是一定会用到jpa的? 是.如果hibernate认为jpa的注解够用,就直接用.否则会弄一个自己的出来作为补充. 2)jpa和hibern ...

  9. springboot 错误处理

    在 java web开发过程中,难免会有一些系统异常或人为产生一些异常.在 RESTful springboot 项目中如何优雅的处理? 分析:在RESTful 风格的springboot 项目中,返 ...

  10. mysql打开binlog

    修改mysql的配置文件,ubuntu下mysql的配置文件存放位置为:/etc/mysql/my.cnf 找到log_bin配置项,指定一个路径: 重启数据库:/etc/init.d/mysql r ...