-1

我知道您可以通过这样做设置为浮点数打印的小数位,%.2f但我只想打印有效数字:

1.33443
1.3
2.00006
4

2 回答 2

2

用这个

double roundTwoDecimals(double d) {
        DecimalFormat twoDForm = new DecimalFormat("#.##");
    return Double.valueOf(twoDForm.format(d));

}

于 2012-10-19T12:01:47.903 回答
0
public static void main(String [] args) {
    float f1 = 1.3344300f;
    float f2 = 1.3000f;
    float f3 = 2.010f;

    System.out.println(f1);;
    System.out.println(f2);;
    System.out.println(f3);;
}

总是打印...

1.33443
1.3
2.01
于 2012-10-19T12:25:16.813 回答