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. Oracle使用order by排序关于null值处理

    select * from dual order by age desc nulls last select * from test order by age asc nulls first sqls ...

  2. Oracle GoldenGate DDL 详细说明 使用手册(较早资料)

    一. 概述 DDL 相关的参数包括:DDL.DDLERROR.DDLOPTIONS.DDLSUBST.DDLTABLE.GGSCHEMA. PURGEDDLHISTORY.PURGEMARKERHIS ...

  3. WinForm 应用程序禁止多个进程运行

    方法一: 禁止多个进程运行 using System; using System.Collections.Generic; using System.Linq; using System.Window ...

  4. [翻译] GMCPagingScrollView

    GMCPagingScrollView https://github.com/GalacticMegacorp/GMCPagingScrollView GMCPagingScrollView is a ...

  5. django的admin后台注册model并显示

    在admin后台注册model并显示其他字段: 修改app下的admin.py,注册model: from .models import vmadmin.site.register(vm) #注册名为 ...

  6. 企业级实时数据文件同步服务_【all】

    全网数据定时备份方案[cron + rsync] [更多参考]全网数据定时备份方案[cron + rsync] 全网数据实时备份方案[inotify,sersync] [更多参考]全网数据实时备份方案 ...

  7. MATLAB 正则表达式(一)(转)

    http://blog.sina.com.cn/s/blog_53f29119010009uf.html 正则表达式这个词上大学的时候就听同寝室的一个家伙常念叨——那家伙当然很厉害啦,现在已经发洋财去 ...

  8. 高性能网站架构缓存——redis集群

    相信你已经对redis有一定的了解,并能够安装上,进行简单的使用了,但是在咱们的实际应用中,使用redis肯定不会使用单机版,不光是redis不能使用单机版,其他的也不会使用,所以今天我们来说一下re ...

  9. #003 React 组件 继承 自定义的组件

    主题:React组件 继承 自定义的 组件 一.需求说明 情况说明: 有A,B,C,D 四个组件,里面都有一些公用的逻辑,比如 设置数据,获取数据,有某些公用的的属性,不想在 每一个 组件里面写这些属 ...

  10. eclipse导入maven工程missing artifact(实际是存在的)错误解决

    找到出错的jar包文件位置,删掉_maven.repositories文件(或用文本编辑器打开,将“>main=”改为“>=”,即删除main,当然main也可能是其他值),然后updat ...