保留有效十进制数的方法
1、
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);
2、两位小数级四舍五入
double f = 111231.5585;
BigDecimal b = new BigDecimal(f);
double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
3.
java.text.DecimalFormat df =new java.text.DecimalFormat("#.00");
df.format(你要格式化的数字);
4.转百分数的方法
import java.text.NumberFormat;
/**
* Double转化为百分数
* @author ganhongliang
* @date 2018年10月1日
*/
public class DoubleToPercentformat {
/**
*
* @param d
* @param IntegerDigits
* @param FractionDigits
* @return String
* @time 2018年10月1日 上午11:09:35
*/
public static String getPercentFormat(double d,int IntegerDigits,int FractionDigits){
NumberFormat nf = java.text.NumberFormat.getPercentInstance();
nf.setMaximumIntegerDigits(IntegerDigits);//小数点前保留几位
nf.setMinimumFractionDigits(FractionDigits);// 小数点后保留几位
String str = nf.format(d);
return str;
}
public static void main(String[] args) {
System.out.println(getPercentFormat(0.9999, 2, 2));
}
}
推荐阅读
-
如何恢复手机中已删除的短信?其实很简单!来了解一下这些有效的方法吧
-
如何恢复删除的手机短信?98% 的人认为这种方法最有效?
-
实现分布式锁的一般方法有哪些?如何使用 Redis 设计分布式锁?可以使用 zk 设计分布式锁吗?这两种实现分布式锁的方法中,哪一种更有效?
-
C 语言编程初学者学习要点:这是学习 C 语言最有效的方法
-
什么是 c 语言十进制转十六进制的方法
-
沃格林:抵制虚假和欺诈的有效方法
-
如何尽量减少 Chrome 浏览器的内存使用量?7 种最有效的方法。
-
计算 IP 地址划分子网中每个子网的子网掩码、可用地址数和 IP 的方法
-
保留有效数字和小数位的 C++ 浮点运算
-
保留小数的问题||有效数字 (C)