package com.clzhang.sample;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat; public class DoubleTest { /**
* 保留两位小数,四舍五入的一个老土的方法
* @param d
* @return
*/
public static double formatDouble1(double d) {
return (double)Math.round(d*100)/100;
}


/**
* The BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion.
* @param d
* @return
*/
public static double formatDouble2(double d) {
// 旧方法,已经不再推荐使用
// BigDecimal bg = new BigDecimal(d).setScale(2, BigDecimal.ROUND_HALF_UP);


// 新方法,如果不需要四舍五入,可以使用RoundingMode.DOWN
BigDecimal bg = new BigDecimal(d).setScale(2, RoundingMode.UP);


return bg.doubleValue();
} /**
* NumberFormat is the abstract base class for all number formats.
* This class provides the interface for formatting and parsing numbers.
* @param d
* @return
*/
public static String formatDouble3(double d) {
NumberFormat nf = NumberFormat.getNumberInstance(); // 保留两位小数
nf.setMaximumFractionDigits(2);


// 如果不需要四舍五入,可以使用RoundingMode.DOWN
nf.setRoundingMode(RoundingMode.UP);


return nf.format(d);
}


/**
* 这个方法挺简单的。
* DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers.
* @param d
* @return
*/
public static String formatDouble4(double d) {
DecimalFormat df = new DecimalFormat("#.00");


return df.format(d);
}


/**
* 如果只是用于程序中的格式化数值然后输出,那么这个方法还是挺方便的。
* 应该是这样使用:System.out.println(String.format("%.2f", d));
* @param d
* @return
*/
public static String formatDouble5(double d) {
return String.format("%.2f", d);
} public static void main(String[] args) {
double d = 12345.67890; System.out.println(formatDouble1(d));
System.out.println(formatDouble2(d));
System.out.println(formatDouble3(d));
System.out.println(formatDouble4(d));
System.out.println(formatDouble5(d));
} }

3. 输出

12345.68
12345.68
12,345.68
12345.68
12345.68

java四舍五入的更多相关文章

  1. 【转】java取整和java四舍五入方法

    java取整和java四舍五入方法 import java.math.BigDecimal; import java.text.DecimalFormat; public class TestGetI ...

  2. java四舍五入BigDecimal和js保留小数点两位

    java四舍五入BigDecimal保留两位小数的实现方法: // 四舍五入保留两位小数System.out.println("四舍五入取整:(3.856)="      + ne ...

  3. Java四舍五入 保留小数

    java 四舍五入保留小数   // 方式一: double f = 3.1516; BigDecimal b = new BigDecimal(f); double f1 = b.setScale( ...

  4. java取整和java四舍五入方法 BigDecimal.setScale()方法详解

    import java.math.BigDecimal; public class TestGetInt { public static void main(String[] args) { doub ...

  5. java取整和java四舍五入方法 转自董俊杰

    import java.math.BigDecimal; import java.text.DecimalFormat; public class TestGetInt{ public static ...

  6. java四舍五入的取舍

    一.保留2位小数,且四舍五入 String re = new java.text.DecimalFormat("#.##").format(3.14555); 结果:3.15 二. ...

  7. JAVA 四舍五入Math.round方法

    今天由于测试场景,利息的计算中涉及小数点的保留.保留的规则是:两位小数+四舍五入方式 使用的语言是JAVA, 看了许多网上的方法.因为最后保留的小数还会进行计算.所以我考虑最好不要保留的结果是Stri ...

  8. java四舍五入保留两位小数4种方法

    4种方法,都是四舍五入,例: import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.NumberF ...

  9. java四舍五入及注意点

    package com.example.newtest.test; import java.math.BigDecimal; import java.math.RoundingMode; import ...

随机推荐

  1. 使用vue做项目

    使用vue做项目需要用到node.js的npm来管理包 所以我们需要先下载node.js然后通过node的npm来管理包  安装完 nodejs后 我们需要执行 npm install vue-cli ...

  2. spider-抓取网页内容(Beautiful soup)

    http://jingyan.baidu.com/article/afd8f4de6197c834e386e96b.html http://cuiqingcai.com/1319.html Windo ...

  3. 浅谈jodaTime 的使用

    第一步:引入依赖: 1 <dependency> 2 <groupId>joda-time</groupId> 3 <artifactId>joda-t ...

  4. 2018 徐州赛区网赛 G. Trace

    题目链接在这里 题意是:按时间先后有许多左下角固定为(0,0),右上角为(xi,yi)的矩形浪潮,每次浪潮会留下痕迹,但是后来的浪潮又会冲刷掉自己区域的老痕,留下新痕迹,问最后留下的痕迹长度为多少? ...

  5. SQL Server中搜索特定的对象

    一.注释中带某关键字的对象 主要用到 sys.tables .sys.columns .sys.procedures  系统对象表以及sys.extended_properties 扩展属性表 --查 ...

  6. 关于for...in和for...of的思考

    关于for...in和for...of的思考 在编写js代码的时候,我们经常性的要对数据集合进行遍历,Array.Object.以及ES6新属性Map.Set,甚至String类型都是可遍历的. 我们 ...

  7. PHP设计模式系列 - 中介者模式

    中介者模式 中介者模式用于开发一个对象,这个对象能够在类似对象相互之间不直接相互的情况下传送或者调解对这些对象的集合的修改.一般处理具有类似属性,需要保持同步的非耦合对象时,最佳的做法就是中介者模式. ...

  8. kvo的observationInfo

    观察者信息的注册: <NSKeyValueObservationInfo 0x600000708d60> ( <NSKeyValueObservance 0x6000009143f0 ...

  9. Objective-C 与命名空间

    http://blog.csdn.net/michelle__/article/details/52528172 Objective-C 在Objective-C应用中的所有类名都必须是全局唯一的.命 ...

  10. 「GXOI / GZOI2019」旅行者

    题目 我还是太傻了 考虑每一条边的贡献,对于一条有向边\((u,v,w)\),我们求出\(k\)个关键点中到\(u\)最近的距离\(dis_1\),以及\(v\)到\(k\)个关键点中最近的距离\(d ...