精确小数点
public class test {
public static void main(String[] args) {
double a = 11.123456;
double aa = round(a,3);
System.out.println(aa);
}
//精确小数点函数
//参数v:代表要操作的对象;
//参数scale:代表要精确的位数
public static double round(double v,int scale)
{
String temp="#0.";
for(int i=0;i<scale;i++)
{
temp+="0";
}
return Double.valueOf(new java.text.DecimalFormat(temp).format(v)).doubleValue();
}
}
public class test {
public static void main(String[] args) {
double a = 11.123456;
double aa = round(a,3);
System.out.println(aa);
}
//精确小数点函数
//参数v:代表要操作的对象;
//参数scale:代表要精确的位数
public static double round(double v,int scale)
{
String temp="#0.";
for(int i=0;i<scale;i++)
{
temp+="0";
}
return Double.valueOf(new java.text.DecimalFormat(temp).format(v)).doubleValue();
}
}
本文介绍了一种在Java中实现的精确小数点运算方法,该方法通过自定义函数实现指定精度的小数点舍入,避免了由于浮点数运算带来的精度误差问题。
397

被折叠的 条评论
为什么被折叠?



