Java中如何取得指定小数位数的double型数据
1.使用NumberFormat
import java.text.*;
public class FormatData {
public static String getResult(double num) {
NumberFormat format = NumberFormat.getNumberInstance();
format.setMaximumFractionDigits(3);
return format.format(num);
}
public static void main(String[] args) {
System.out.println(getResult(0.0010)); }
}
2.通过扩大后缩小的方法实现
public static double getDouble(int n,double a)
{
double a2=a*n;
int b=(int)a2;
double c=(double)b;
double d=c/n
return d;
}