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 ...
随机推荐
- Delphi Language Overview
Delphi is a high-level, compiled, strongly typed language that supports structured and object-orient ...
- eclipse/myeclipse清除workspace
打开Eclipse后,选择功能菜单里的 Windows -> Preferences->, 弹出对话框后,选择 General -> Startup and Shutdownwor ...
- 跨终端Web
1.终端vs设备 H5页面运行在同一设备的不同终端下. (1)Web浏览器. (2)微信.QQ浏览器. (3)移动App的Webview. (4)TV机顶盒. 2.跨终端的实现方式 (1)响应式 存在 ...
- Effective Java(1)-创建和销毁对象
Effective Java(1)-创建和销毁对象
- CC2530学习路线-基础实验-定时器控制LED灯亮灭(3)
目录 1. 前期预备知识 1.1 定时器中断触发 1.2 相关寄存器 1.3 寄存器相关问题 1.4 T1.T3定时器初始化流程 2 程序及代码 THE END 1. 前期预备知识 1.1 定时器中断 ...
- C#默认以管理员身份运行程序实现代码
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; names ...
- oracle 游标简单案例
oracle 游标简单案例 一.案例: DECLARE IDO NUMBER; DABH CHAR); t_count ); CURSOR TJ_CURSOR IS SELECT IDO,DABH ...
- Mac OS 10.12 - ”ln: /usr/bin/tclsh: Operation not permitted“错误的解决方法!!
我在对"/usr/bin/"进行创建链接时候,出现错误:”ln: /usr/bin/tclsh: Operation not permitted“,这个错误的原因是Rootless ...
- Concurrent包工具类使用
一.读写锁 传统的同步锁就是独占式锁,当线程使用资源时候保持独占,无论读写.当人们发现请求队列(假设)中相邻请求为读-读的时候,阻塞是一种浪费资源的操作.比如公告板,所有路过的人(请求)都是读操作,并 ...
- NSTimer、performSelector 函数没有被调用的原因
performSelector 指定的方法没有被调用 Invokes a method of the receiver on the current thread using the default ...