double x1 = 0.026;
String format = String.format("%.2f", x1);
System.out.println(format);
String format = String.format("%.nf", x1);

n : 表示保留的位数(四舍五入)

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));
}

}

 

java 四舍五入 保留n为数的更多相关文章

  1. Java四舍五入 保留小数

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

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

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

  3. java 四舍五入保留小数

    // 方式一: double f = 3.1516; BigDecimal b = new BigDecimal(f); double f1 = b.setScale(2, BigDecimal.RO ...

  4. java 四舍五入保留两位小数

    // 保留两位小数 System.out.println(Double.parseDouble(String.format("%.2f", 55.5454545454))); // ...

  5. java 四舍五入 保留两位小数

    1. 格式化字符串 java.text.DecimalFormat df = new java.text.DecimalFormat("#0.00"); float val=Flo ...

  6. java 四舍五入 保留俩位小数

    public static void main(String[] args) {              String str="0";              BigDeci ...

  7. java四舍五入保留几位小数

    double d = 3.1415926; String result = String.format("%.2f", d); // %.2f %. 表示 小数点前任意位数 2 表 ...

  8. Java四舍五入保留n位小数的常用写法

    1. 使用BigDecimal double v = 1.233; double res = new BigDecimal(v).setScale(2, RoundingMode.HALF_UP).d ...

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

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

随机推荐

  1. 模拟电磁曲射炮_H题 方案分析【2019年电赛】【刘新宇qq522414928】

    请查看我的有道云笔记: 文档:电磁曲射炮分析.note链接:http://note.youdao.com/noteshare?id=26f6b6febc04a8983d5efce925e92e21

  2. 进阶 Linux基本命令-2

    mkdir -p /a/b/c/d                 -p 循环创建目录yum install tree -y                                      ...

  3. jQuery的attr和prop属性

    <div id="div1"></div> attr: 首先是一个参数的attr. $("#div").attr("id&qu ...

  4. Python玩转人工智能最火框架 TensorFlow应用实践 学习 教程

    随着 TensorFlow 在研究及产品中的应用日益广泛,很多开发者及研究者都希望能深入学习这一深度学习框架.而在昨天机器之心发起的框架投票中,2144 位参与者中有 1441 位都在使用 Tenso ...

  5. umditor删除域名,配置为绝对路径

    getAllPic: function (sel, $w, editor) { var me = this, arr = [], $imgs = $(sel, $w); $.each($imgs, f ...

  6. 01-Vue初学习

    1. Vue下载 (1)网址:https://cn.vuejs.org/v2/guide/installation.html (2)点击开发版本,下载完成是一个 Vue.js 2. 使用 (1)创建文 ...

  7. Spring PropertyPlaceholderConfigurer类载入外部配置

    2019独角兽企业重金招聘Python工程师标准>>> 通常在Spring项目中如果用到配置文件时,常常会使用org.springframework.beans.factory.co ...

  8. CodeForces - 1245A Good ol' Numbers Coloring (思维)

    Codeforces Round #597 (Div. 2 Consider the set of all nonnegative integers: 0,1,2,-. Given two integ ...

  9. python-CSV格式清洗与转换、CSV格式列变换、CSV格式数据清洗【数据读入的三种方法】【strip、replace、split、join函数的使用】

    1)CSV格式清洗与转换 描述 附件是一个CSV格式文件,提取数据进行如下格式转换:‪‬‪‬‬‪‬‮‬‪‬‭‬ (1)按行进行倒序排列:‪‬‪‬‪‬‪‬‪‬‮‬‬‪‬‮‬‪‬‭‬ (2)每行数据倒序排 ...

  10. 前端——Vue-cli 通过UI页面创建项目

    在使用该教程创建项目时请先安装vue ui,具体安装方法请百度 1.打开CMD,输入vue ui 2.点击创建按钮,选择项目目录 3.填写项目名 4.配置项目 选择项目所需要的模块