java 四舍五入 保留n为数
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为数的更多相关文章
- Java四舍五入 保留小数
java 四舍五入保留小数 // 方式一: double f = 3.1516; BigDecimal b = new BigDecimal(f); double f1 = b.setScale( ...
- java四舍五入保留两位小数4种方法
4种方法,都是四舍五入,例: import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.NumberF ...
- java 四舍五入保留小数
// 方式一: double f = 3.1516; BigDecimal b = new BigDecimal(f); double f1 = b.setScale(2, BigDecimal.RO ...
- java 四舍五入保留两位小数
// 保留两位小数 System.out.println(Double.parseDouble(String.format("%.2f", 55.5454545454))); // ...
- java 四舍五入 保留两位小数
1. 格式化字符串 java.text.DecimalFormat df = new java.text.DecimalFormat("#0.00"); float val=Flo ...
- java 四舍五入 保留俩位小数
public static void main(String[] args) { String str="0"; BigDeci ...
- java四舍五入保留几位小数
double d = 3.1415926; String result = String.format("%.2f", d); // %.2f %. 表示 小数点前任意位数 2 表 ...
- Java四舍五入保留n位小数的常用写法
1. 使用BigDecimal double v = 1.233; double res = new BigDecimal(v).setScale(2, RoundingMode.HALF_UP).d ...
- java四舍五入BigDecimal和js保留小数点两位
java四舍五入BigDecimal保留两位小数的实现方法: // 四舍五入保留两位小数System.out.println("四舍五入取整:(3.856)=" + ne ...
随机推荐
- hashlib的md5计算
hashlib的md5计算 hashlib概述 涉及加密服务:Cryptographic Services 其中 hashlib是涉及 安全散列 和 消息摘要 ,提供多个不同的加密算法借口,如SHA1 ...
- pytorch 文本情感分类和命名实体识别NER中LSTM输出的区别
文本情感分类: 文本情感分类采用LSTM的最后一层输出 比如双层的LSTM,使用正向的最后一层和反向的最后一层进行拼接 def forward(self,input): ''' :param inpu ...
- Leo2DNT(雷傲论坛转DiscuzNT)1.0转换程序发布
数据转换程序 雷傲论坛(Leobbs4.x) -> Discuz!NT V1.0 本转换程序基于Leobbs4.x设计 声明: 1.本程序只对数据作转换,不会对原来的雷傲论坛(数据 ...
- Norwegian Wood
0 前言 <挪威的森林>是村上春树很有名的一部小说,但我想大多数人阅读的时候都只是把书名当作一个符号,而不是作为故事去追究. 我国台湾知名文学评论家杨照先生说过:村上的书里有太多太多典故, ...
- ACM-ICPC 2019 山东省省赛 M Sekiro
Sekiro: Shadows Die Twice is an action-adventure video game developed by FromSoftware and published ...
- Visual Studio Code 缩放设置
Windows下的软件的操作都很类似,所以刚开始使用vs code的时候习惯性地使用Ctrl+鼠标滚轮进行缩放,然而在vs code上不管用. 在vs code中有两类缩放: 一.整体缩放:包括菜单栏 ...
- MYSQL数据库配置安装、重置密码以及工具连接
一.下载mysql安装包 下载地址:https://dev.mysql.com/downloads/mysql/ 下载解压好之后,就是一个文件夹的形式. 二.配置环境变量 环境变量的配置,就是把MyS ...
- search(9)- elastic4s logback-appender
前面写了个cassandra-appender,一个基于cassandra的logback插件.正是cassandra的分布式数据库属性才合适作为akka-cluster-sharding分布式应用的 ...
- weak_ptr
#include <iostream> #include <memory> using namespace std; int main(int argc, char **arg ...
- E. A Simple Task
E. A Simple Task 这个题目的意思是 给你一个由小写字母组成的字符串,有两种操作 i j k 如果k==1 那么就是就把i 到 j 的这个区间非递减排序. i j k如果k==2 那么就 ...