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. react typescript jest config (一)

    1. initialize project create a folder project Now we'll turn this folder into an npm package. npm in ...

  2. pytorch 矩阵数据增加维度unsqueeze和降低维度squeeze

    增加一个维度 out.unsqueeze(-1) 降低一个维度 out.squeeze(dim=1)

  3. 进程管理工具 Supervisor

    要想在终端后台常驻进程,首先想到的是在命令后加 & 符号,来达到隐藏程序在后台的目的,尽管看起来进程已经在后台运行了,实际上终端会话关闭时进程还是会被 kill 掉,这种问题一般是采用搭配 n ...

  4. curl发送多维数组

    //通过curl模拟post的请求: function SendDataByCurl($url,$data=array()){ //对空格进行转义 $url = str_replace(' ','+' ...

  5. springboot+dubbo简单分布式RPC调用demo

    使用springboot+dubbo搭建RPC入门案例 本文背景简述: 最近在学习公司的一套RPC框架,初步接触的时候感觉挺复杂的.但是知道其原理肯定是和dubbo很相似的,毕竟都是RPC框架嘛,只是 ...

  6. Metasploit学习笔记(一)

    1.更新 apt-get update:更新源 apt-get upgrade:更新软件包 apt-get dist-upgrade:升级系统 2. Metasploit基础 2.1专业名词 Auxi ...

  7. Shodan使用简述

    申明 本文只做相关介绍,使用者应当严格自律,承诺遵守法律法规     Shodan,一款互联网下的可怕搜索引擎.它的可怕之处在于Shodan可以搜索各种在线的网络设备.比如:摄像头.路由器.打印机.服 ...

  8. NumPy学习指南(第2版)

    第一章 NumPy快速入门 首先,我们将介绍如何在不同的操作系统中安装NumPy和相关软件,并给出使用NumPy的简单示例代码. 然后,我们将简单介绍IPython(一种交互式shell工具). 如前 ...

  9. spark下dataframe转为rdd格式

    dataframe可以实现很多操作,但是存储到本地的时候,只能存 parquest格式 需要存储源格式,需要转换为rdd类型 将dataframe中的每一行都map成有逗号相连的string,就变为了 ...

  10. idea jdk版本切换

    为什么80%的码农都做不了架构师?>>>   打开file-peoject structure,或者 改完project后,点击models里面的sources 和dependenc ...