概述
https://blog.csdn.net/qq_48736958/article/details/115787037?spm=1001.2101.3001.6650.4&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-4-115787037-blog-121975948.pc_relevant_antiscanv2&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-4-115787037-blog-121975948.pc_relevant_antiscanv2&utm_relevant_index=6
java的几种保留小数位数的方法
法一:DecimalFormat
public class DecimalFormatDemo {
public static void main(String[] args) {
double decimal=2.15545422;
DecimalFormat df=new DecimalFormat("#.0000");
String format = df.format(decimal);
double v = Double.parseDouble(format);
System.out.println(v);
}
}
法二:String.format
public class DecimalFormatDemo {
public static void main(String[] args) {
double decimal=2.15555422;
String format = String.format("%.3f", decimal);
double v = Double.parseDouble(format);
System.out.println(v);
}
}
法三:NumberFormat
public class DecimalFormatDemo {
public static void main(String[] args) {
double decimal=2.15555422;
/*DecimalFormat df=new DecimalFormat("#.0000");
String format = df.format(decimal);
double v = Double.parseDouble(format);
System.out.println(v);*/
/*String format = String.format("%.3f", decimal);
double v = Double.parseDouble(format);
System.out.println(v);*/
NumberFormat nf=NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(3);
String format = nf.format(decimal);
double v = Double.parseDouble(format);
System.out.println(v);
}
}
法四:BigDecimal
- ROUND_UP
只要保留位数之后有一位为0就进位,且与正负无关
public class DecimalFormatDemo {
public static void main(String[] args) {
BigDecimal a = new BigDecimal(3.6666666666);
BigDecimal b = new BigDecimal(-3.650000001);
System.out.println(a.setScale(2,BigDecimal.ROUND_UP));
System.out.println(b.setScale(2,BigDecimal.ROUND_UP));
}
}
3.660000001——>3.67
3.666666666——>3.67
-3.65000001——>-3.66
-3.65000000——>3.65
- ROUND_DOWN
向下保留,不管保留位数后面是多少,统统舍去
public class DecimalFormatDemo {
public static void main(String[] args) {
BigDecimal a = new BigDecimal(3.66666666);
BigDecimal b = new BigDecimal(-3.66666666);
System.out.println(a.setScale(2,BigDecimal.ROUND_DOWN));
System.out.println(b.setScale(2,BigDecimal.ROUND_DOWN));
}
}
3.66
-3.66
- ROUND_CEILING 向正无穷方向舍入
public class DecimalFormatDemo {
public static void main(String[] args) {
BigDecimal a = new BigDecimal(3.66666666);
BigDecimal b = new BigDecimal(-3.66666666);
System.out.println(a.setScale(2,BigDecimal.ROUND_CEILING));
System.out.println(b.setScale(2,BigDecimal.ROUND_CEILING));
}
}
3.67
-3.66
- ROUND_FLOOR 向负无穷方向舍入
public class DecimalFormatDemo {
public static void main(String[] args) {
BigDecimal a = new BigDecimal(3.66666666);
BigDecimal b = new BigDecimal(-3.66666666);
System.out.println(a.setScale(2,BigDecimal.ROUND_FLOOR));
System.out.println(b.setScale(2,BigDecimal.ROUND_FLOOR));
}
}
3.66
-3.67
- ROUND_HELF_UP:数字保留法(四舍五入)
public class DecimalFormatDemo {
public static void main(String[] args) {
BigDecimal a = new BigDecimal(3.66666666);
BigDecimal b = new BigDecimal(-3.66666666);
System.out.println(a.setScale(2,BigDecimal.ROUND_HALF_DOWN));
System.out.println(b.setScale(2,BigDecimal.ROUND_HALF_DOWN));
}
}
3.67
-3.67
最后
以上就是洁净小白菜为你收集整理的java的几种保留小数位数的方法的全部内容,希望文章能够帮你解决java的几种保留小数位数的方法所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复