(后台)Java:对double值进行四舍五入,保留两位小数的几种方法
mport java.text.DecimalFormat;
DecimalFormat df = new DecimalFormat("######0.00"); double d1 = 3.23456
double d2 = 0.0;
double d3 = 2.0;
df.format(d1);
df.format(d2);
df.format(d3);
3个结果分别为:
3.23
0.00
2.00
java保留两位小数问题:
方式一:
四舍五入
double f = 111231.5585;
BigDecimal b = new BigDecimal(f);
double f1 = b.setScale(, BigDecimal.ROUND_HALF_UP).doubleValue();
保留两位小数
方式二:
java.text.DecimalFormat df =new java.text.DecimalFormat("#.00");
df.format(你要格式化的数字);
例:
new java.text.DecimalFormat("#.00").format(3.1415926)
#.00 表示两位小数 #.0000四位小数 以此类推...
方式三:
double d = 3.1415926;
String result = String .format("%.2f");
%.2f %. 表示 小数点前任意位数 2 表示两位小数 格式后的结果为f 表示浮点型
NumberFormat ddf1=NumberFormat.getNumberInstance() ;
void setMaximumFractionDigits(int digits)
digits 显示的数字位数
为格式化对象设定小数点后的显示的最多位,显示的最后位是舍入的
import java.text.* ;
import java.math.* ;
class TT
{
public static void main(String args[])
{ double x=23.5455;
NumberFormat ddf1=NumberFormat.getNumberInstance() ; ddf1.setMaximumFractionDigits();
String s= ddf1.format(x) ;
System.out.print(s);
}
}
import java.text.*;
DecimalFormat df=new DecimalFormat(".##");
double d=1252.2563;
String st=df.format(d);
System.out.println(st);
下面是百度
1. 功能
将程序中的double值精确到小数点后两位。可以四舍五入,也可以直接截断。
比如:输入12345.6789,输出可以是12345.68也可以是12345.67。至于是否需要四舍五入,可以通过参数来决定(RoundingMode.UP/RoundingMode.DOWN等参数)。
2. 实现代码

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:对double值进行四舍五入,保留两位小数的几种方法的更多相关文章
- Java:对double值进行四舍五入,保留两位小数的几种方法
1. 功能 将程序中的double值精确到小数点后两位.可以四舍五入,也可以直接截断. 比如:输入12345.6789,输出可以是12345.68也可以是12345.67.至于是否需要四舍五入,可以通 ...
- Double值保留两位小数的四种方法
public class DoubleTest { //保留两位小数第三位如果大于4会进一位(四舍五入) double f = 6.23556; /** *使用精确小数BigDecimal */ pu ...
- php保留两位小数的3种方法
<?php $num = 8.16789; //第一种:利用round()对浮点数进行四舍五入 echo round($num,2).PHP_EOL; //8.17 //第二种:利用sprint ...
- Python中保留两位小数的几种方法
https://blog.csdn.net/Jerry_1126/article/details/85009810 保留两位小数,并做四舍五入处理方法一: 使用字符串格式化>>> a ...
- JAVA 保留两位小数的四种方法
import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.NumberFormat; publiccl ...
- PHP保留两位小数的几种方法
$num = 10.4567; //第一种:利用round()对浮点数进行四舍五入 echo round($num,2); //10.46 //第二种:利用sprintf格式化字符串 $format_ ...
- JS保留两位小数的几种方法
四舍五入 以下处理结果会四舍五入: var num =2.446242342; num = num.toFixed(2); // 输出结果为 2.45 不四舍五入 以下处理结果不会四舍五入: 第一种, ...
- php 基础 PHP保留两位小数的几种方法
$num = 10.4567; //第一种:利用round()对浮点数进行四舍五入 echo round($num,2); //10.46 //第二种:利用sprintf格式化字符串 $format_ ...
- 【0624课外作业】将一个double类型的小数,四舍五入保留两位小数
package com.work0624; /** * 课外作业 *将一个double类型的小数,四舍五入保留两位小数 * @author L * */ import java.util.Scanne ...
随机推荐
- docker,docker-compose部署服务器
搭建服务器 docker 是一种容器技术,作用是用来快速部署服务,docker-compose 是用来做docker 的多容器控制. 简单的来说:docker-compose即为一种自动化部署服务. ...
- Python基础入门教程(3)
人生苦短,我学Pyhton Python(英语发音:/ˈpaɪθən/), 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于199 ...
- [EXP]Drupal < 8.5.11 / < 8.6.10 - RESTful Web Services unserialize() Remote Command Execution (Metasploit)
## # This module requires Metasploit: https://metasploit.com/download # Current source: https://gith ...
- 【PaddlePaddle】自然语言处理:句词预测
前言 预测词汇的相关性算是自然语言中的HelloWolrd.本文主要根据百度PaddlePaddle示例word2vec,对句子中下一个单词的预测.该示例使用4个词语来预测下一个词. 1. 数据集以及 ...
- python 使用PyInstaller将程序打包
PyInstaller可以用来打包python应用程序,打包完的程序就可以在没有安装Python解释器的机器上运行了.类似于C#窗体程序使用Setup Factory 9 Trial进行打包. 安装: ...
- js按钮 防重复提交
给html 按钮加id属性 例: <button id="addBtn" onclinck="check()"> </button&g ...
- Nginx + Keepalived负载均衡
第一步: 下载keepalived地址:http://www.keepalived.org/download.html 解压安装: tar -zxvf keepalived-1.2.18.tar.gz ...
- zookeeper ZAB协议 Follower和leader源码分析
Follower处理逻辑 void followLeader() throws InterruptedException { //... try { //获取leader server QuorumS ...
- leetcode — integer-to-roman
/** * Source : https://oj.leetcode.com/problems/integer-to-roman/ * * Created by lverpeng on 2017/7/ ...
- 实验吧 貌似有点难 伪造ip
解题链接: http://ctf5.shiyanbar.com/phpaudit/ 解答: 点击View the source code —>代码显示IP为1.1.1.1即可得到KEY—> ...