Java指定保留小数位数的方法
package com.qiyuan.util; import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat; public class DecimalUtils { /**
* (1)按四舍五入保留指定小数位数,位数不够用0补充(一般不这么用)
* @param o:格式化前的小数
* @param newScale:保留小数位数
* @return 格式化后的小数
*/
/*public static String formatDecimalWithZero(Object o, int newScale) {
System.out.println("============================方法1================================");
return String.format("%." + newScale + "f", o);
}*/ /**
* (2)按四舍五入保留指定小数位数,位数不够用0补充
* @param d:格式化前的小数
* @param newScale:保留小数位数
* @return 格式化后的小数
*/
public static String formatDecimalWithZero(double d, int newScale) {
System.out.println("============================方法2================================");
String pattern = "0.";
for (int i = 0; i < newScale; i++) {
pattern += "0";
}
DecimalFormat df = new DecimalFormat(pattern);
return df.format(d);
} /**
* (3)按四舍五入保留指定小数位数,位数不够用0补充
* @param d 格式化前的小数 String形式
* @param newScale 保留小数位数
* @return 格式化后的小数
*/
public static String formatDecimalWithZero(String d, int newScale) {
System.out.println("============================方法3================================");
String pattern = "0.";
for (int i = 0; i < newScale; i++) {
pattern += "0";
}
DecimalFormat df = new DecimalFormat(pattern);
return df.format(Double.valueOf(d));
} /**
* (4)按四舍五入保留指定小数位数,小数点后仅保留有效位数
* @param d 格式化前的小数
* @param newScale 保留小数位数
* @return 格式化后的小数
*/
public static String formatDecimal(double d, int newScale) {
System.out.println("============================方法4================================");
String pattern = "#.";
for (int i = 0; i < newScale; i++) {
pattern += "#";
}
DecimalFormat df = new DecimalFormat(pattern);
return df.format(d);
} /**
* (5)按四舍五入保留指定小数位数,小数点后仅保留有效位数
* @param d 格式化前的小数
* @param newScale 保留小数位数
* @return 格式化后的小数
*/
public static String formatDecimal(String d, int newScale) {
System.out.println("============================方法5================================");
String pattern = "#.";
for (int i = 0; i < newScale; i++) {
pattern += "#";
}
DecimalFormat df = new DecimalFormat(pattern);
return df.format(Double.valueOf(d));
} /**
* (6)按指定舍入模式保留指定小数位数
* @param d 格式化前的小数
* @param newScale 保留小数位数
* @param roundingMode 舍入模式
* (RoundingMode.UP始终进一/DOWN直接舍弃/
* CEILING正进负舍/FLOOR正舍负进/
* HALF_UP四舍五入/HALF_DOWN五舍六进/
* HALF_EVEN银行家舍入法/UNNECESSARY抛出异常)
* @return 格式化后的小数
*/
public static double formatDecimal(double d, int newScale, RoundingMode roundingMode) {
System.out.println("============================方法6================================");
BigDecimal bd = new BigDecimal(d).setScale(newScale, roundingMode);
return bd.doubleValue();
} /**
* (7)按指定舍入模式保留指定小数位数
* @param d 格式化前的小数
* @param newScale 保留小数位数
* @param roundingMode 舍入模式
* (RoundingMode.UP始终进一/DOWN直接舍弃/
* CEILING正进负舍/FLOOR正舍负进/
* HALF_UP四舍五入/HALF_DOWN五舍六进/
* HALF_EVEN银行家舍入法/UNNECESSARY抛出异常)
* @return 格式化后的小数
*/
public static double formatDecimal(String d, int newScale, RoundingMode roundingMode) {
System.out.println("============================方法7================================");
BigDecimal bd = new BigDecimal(Double.valueOf(d)).setScale(newScale, roundingMode);
return bd.doubleValue();
} public static void main(String[] args) {
System.out.println("测试2=========按四舍五入保留指定小数位数,位数不够用0补充"+DecimalUtils.formatDecimalWithZero(123.456,5));
System.out.println("测试2=========按四舍五入保留指定小数位数,位数不够用0补充"+DecimalUtils.formatDecimalWithZero(123.45678901234,5));
System.out.println("测试3=========按四舍五入保留指定小数位数,位数不够用0补充"+DecimalUtils.formatDecimalWithZero("123.456",5));
System.out.println("测试3=========按四舍五入保留指定小数位数,位数不够用0补充"+DecimalUtils.formatDecimalWithZero("123.45678901234",5));
System.out.println("测试4=========按四舍五入保留指定小数位数,小数点后仅保留有效位数"+DecimalUtils.formatDecimal(123.456,5));
System.out.println("测试4=========按四舍五入保留指定小数位数,小数点后仅保留有效位数"+DecimalUtils.formatDecimal(123.45678901234,5));
System.out.println("测试5=========按四舍五入保留指定小数位数,小数点后仅保留有效位数"+DecimalUtils.formatDecimal("123.456",5));
System.out.println("测试5=========按四舍五入保留指定小数位数,小数点后仅保留有效位数"+DecimalUtils.formatDecimal("123.45678901234",5));
System.out.println("测试6=========按始终进一模式保留指定小数位数"+DecimalUtils.formatDecimal(123.45678901234,8,RoundingMode.UP));
System.out.println("测试6=========按直接截断模式保留指定小数位数"+DecimalUtils.formatDecimal(123.45678901234,8,RoundingMode.DOWN));
System.out.println("测试6=========按正进负舍模式保留指定小数位数"+DecimalUtils.formatDecimal(123.45678901234,8,RoundingMode.CEILING));
System.out.println("测试6=========按正进负舍模式保留指定小数位数"+DecimalUtils.formatDecimal(-123.45678901234,8,RoundingMode.CEILING));
System.out.println("测试6=========按正舍负进模式保留指定小数位数"+DecimalUtils.formatDecimal(123.45678901234,8,RoundingMode.FLOOR));
System.out.println("测试6=========按正舍负进模式保留指定小数位数"+DecimalUtils.formatDecimal(-123.45678901234,8,RoundingMode.FLOOR));
System.out.println("测试6=========按四舍五入模式保留指定小数位数"+DecimalUtils.formatDecimal(123.45678901234,8,RoundingMode.HALF_UP));
System.out.println("测试6=========按四舍五入模式保留指定小数位数"+DecimalUtils.formatDecimal(123.45678901534,8,RoundingMode.HALF_UP));
System.out.println("测试6=========按五舍六进模式保留指定小数位数"+DecimalUtils.formatDecimal(123.45678901534,8,RoundingMode.HALF_DOWN));
System.out.println("测试6=========按五舍六进模式保留指定小数位数"+DecimalUtils.formatDecimal(123.45678901634,8,RoundingMode.HALF_DOWN));
System.out.println("测试6=========按银行家舍入法模式保留指定小数位数"+DecimalUtils.formatDecimal(123.45678901434,8,RoundingMode.HALF_EVEN));
System.out.println("测试7=========按始终进一模式保留指定小数位数"+DecimalUtils.formatDecimal("123.45678901234",8,RoundingMode.UP));
System.out.println("测试7=========按直接截断模式保留指定小数位数"+DecimalUtils.formatDecimal("123.45678901234",8,RoundingMode.DOWN));
System.out.println("测试7=========按正进负舍模式保留指定小数位数"+DecimalUtils.formatDecimal("123.45678901234",8,RoundingMode.CEILING));
System.out.println("测试7=========按正进负舍模式保留指定小数位数"+DecimalUtils.formatDecimal("-123.45678901234",8,RoundingMode.CEILING));
System.out.println("测试7=========按正舍负进模式保留指定小数位数"+DecimalUtils.formatDecimal("123.45678901234",8,RoundingMode.FLOOR));
System.out.println("测试7=========按正舍负进模式保留指定小数位数"+DecimalUtils.formatDecimal("-123.45678901234",8,RoundingMode.FLOOR));
System.out.println("测试7=========按四舍五入模式保留指定小数位数"+DecimalUtils.formatDecimal("123.45678901234",8,RoundingMode.HALF_UP));
System.out.println("测试7=========按四舍五入模式保留指定小数位数"+DecimalUtils.formatDecimal("123.45678901534",8,RoundingMode.HALF_UP));
System.out.println("测试7=========按五舍六进模式保留指定小数位数"+DecimalUtils.formatDecimal("123.45678901534",8,RoundingMode.HALF_DOWN));
System.out.println("测试7=========按五舍六进模式保留指定小数位数"+DecimalUtils.formatDecimal("123.45678901634",8,RoundingMode.HALF_DOWN));
System.out.println("测试7=========按银行家舍入法模式保留指定小数位数"+DecimalUtils.formatDecimal("123.45678901434",8,RoundingMode.HALF_EVEN));
} }
Java指定保留小数位数的方法的更多相关文章
- C#保留小数位数的方法
1.System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo();provi ...
- JAVA中保留小数的多种方法
// 方式一:double f = 3.1516;BigDecimal b = new BigDecimal(f);double f1 = b.setScale(2, BigDecimal.ROUND ...
- C# decimal保留指定的小数位数,不四舍五入
decimal保留指定位数小数的时候,.NET自带的方法都是四舍五入的. 项目中遇到分摊金额的情况,最后一条的金额=总金额-已经分摊金额的和. 这样可能导致最后一条分摊的时候是负数,所以自己写了一个保 ...
- java实验三——求平均数,数组排序(有关java保留小数位数,由于编译器版本未到1.5导致的报错format函数第二个参数不对,要求是Object[])
package hello; import java.util.Arrays; public class 实验三更正版 { public static void main(String[] args) ...
- Java四舍五入 保留小数
java 四舍五入保留小数 // 方式一: double f = 3.1516; BigDecimal b = new BigDecimal(f); double f1 = b.setScale( ...
- 总结C#保留小数位数及百分号处理
方法一: ); 方法二: Math.Round() 方法三: double dbdata = 0.55555; string str1 = dbdata.ToString("f2" ...
- C#保留小数位数
1.System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo(); prov ...
- C#double转化成字符串 保留小数位数, 不以科学计数法的形式出现
在C#中大家都会遇到这种情况 double类型的数据,需要格式化(保留N未有效数字)或者是保留N为小数等情况,我们往往采取double.tostring("参数");的方法.下 ...
- 总结C#保留小数位数
2.C#保留小数位N位,四舍五入 . decimal d= decimal.Round(decimal.Parse("0.55555"),2); 3.C#保留小数位N位四舍五入 M ...
随机推荐
- js插件开发的一些感想和心得
起因 如果大家平时做过一些前端开发方面的工作,一定会有这样的体会:页面需要某种效果或者插件的时候,我们一般会有两种选择:1.上网查找相关的JS插件,学习其用法2.自己造轮子,开发插件. 寻找存在的插件 ...
- 设计模式--Singleton_(1)(C#版)
今天我们来探索一下Singleton设计模式的实现及应用场景. Singleton模式属于Creational Type(创建型)设计模式的一种.该模式一般用于确保在应用中仅创建一个某类的instan ...
- JS 中的数据类型
简介 JavaScript 语言的每一个值,都属于某一种数据类型.JavaScript 的数据类型,共有七种 数值(number):整数和小数,比如1和3.14 字符串(string):文本 布尔值( ...
- httpclient 用法
链接地址 https://www.cnblogs.com/mykcode/p/7833090.html 在程序用调用 Http 接口.请求 http 资源.编写 http 爬虫等的时候都需要在程序集中 ...
- Linux下运行crm项目
虚拟环境运行crm项目 1.进入虚拟环境 2.解决crm项目运行所需的依赖环境 1.手动解决 pip3 install django==1.11.14 pip3 install pymysql pip ...
- 一个Unix内核级别漏洞(一)
翻译原创稿件,prison整理翻译,首发ichunqiu,原地址:http://lsd-pl.net/kernelvuln.pdf 这是一篇关于Unix内核级别漏洞的paper,由某团队发布在一次黑客 ...
- 在centOS 7 中安装 MySQL
知道来源:https://www.cnblogs.com/bigbrotherer/p/7241845.html 1 下载并安装MySQL官方的 Yum Repository [root@localh ...
- (6)Oracle基础--简单查询
.基本查询语句 SELECT [DISTINCT] column_name1,... | * FROM table_name [WHERE conditions]; P: DISTINCT关键字的作 ...
- centos 6 下,zephir的安装和使用
centos 6 下,zephir的安装和使用 zephir或许会开启一个新的PHP编写方式. 在这之前,如果我们要编写php的扩展,一般都是c++/clang/vc等等. 但是现在,我们有了新的选择 ...
- DCL,即Double Check Lock,中卫双重检查锁定。
DCL,即Double Check Lock,中卫双重检查锁定. [Java并发编程]之十六:深入Java内存模型——happen-before规则及其对DCL的分析(含代码) 关于单例.关于DCL: ...