ImportJava. Text. decimalformat;
Public ClassNumberfarmat {
Public Static VoidMain (string [] ARGs ){
DoubleSD = 23.2558896635;
// The first method is 10000.0. after the decimal point, only the decimal points are retained, which has nothing to do with the number of digits.
DoubleD1 = (Double) (Math.Round(SD x 10000)/10000.0000000000 );
DoubleD2 = (Double) (Math.Round(SD x 10000)/10000.0 );
System.Out. Println ("4-digit decimal test:" + D1 );
System.Out. Println ("4-digit decimal test:" + D2 );
// Method 2
Decimalformat df2 =NewDecimalformat ("###. 00 ");
Decimalformat df3 =NewDecimalformat ("#. 000 ");
System.Out. Println ("3 decimal places:" + df3.format (SD ));
System.Out. Println ("2 decimal places:" + df2.format (SD ));
}
}
The running result is as follows:
4-digit decimal test: 23.2559
4-digit decimal test: 23.2559
3 decimal places: 23.256
2 decimal places: 23.26