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. 《剑指Offer》- 连续子数组的最大和或最小和

    前言 本文是<剑指Offer>系列(JavaScript版)的第一篇,题目是"连续子数组的最大和或最小和". 话不多说,开始"打怪"修炼... 一. ...

  2. JS面向对象编程之封装

    来源:https://segmentfault.com/a/1190000015843072 我们所熟知的面向对象语言如 C++.Java 都有类的的概念,类是实例的类型模板,比如Student表示学 ...

  3. thinkphp--多个id查询

    $feedback_list = $feedback -> where( array("member_id"=>array("in", " ...

  4. apt 安装 版本

    1. 通过apt-get安装指定版本软件 apt-get install package=version 2. 查询指定软件有多少个版本 2.1 通过网站搜索https://packages.ubun ...

  5. LeetCode7-ReverseInteger

    LeetCode7-ReverseInteger LeetCodeeasyOverflow 题目 题目所在链接为 LeetCode-7:ReverseInteger 题目描述 给出一个32位的有符号整 ...

  6. (转)如何学好C++语言

    原文:http://coolshell.cn/articles/4119.html   作者:陈皓 昨天写了一篇如何学好C语言,就有人回复问我如何学好C++,所以,我把我个人的一些学习经验写在这里,希 ...

  7. @SessionAttributes 和 @SessionAttribute的区别

    @SessionAttributes 和 @SessionAttribute的区别 Spring MVC中有两个长得非常像的注解:@SessionAttributes 和 @SessionAttrib ...

  8. css-position之fixed vs sticky

    css-position之fixed  vs sticky fixed(固定定位) 元素相对于浏览器窗口是固定的,即使是窗口滚动,元素也是固定的 sticky(粘性定位) 基于用户滚定动来进行定位的, ...

  9. flex学习

            今天看视频学习的时候,发现css有一个 flex 的属性,非常的好用,为了让自己能够熟记,特意来分享一下flex的用法. 首先,采用Flex布局的元素,称为Flex容器(flex co ...

  10. 基于opencv的人脸识别程序

    1. 解析opencv自带人脸识别源码(……/opencv-3.1.0/samples/cpp/facedetect.cpp) @ 操作系统:Ubuntu 15.04 OpenCV版本:3.1.0 # ...