import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
/**
* 四捨五入
*
@author killer
*
*/
public class NumberToRoundTest {
public static void main(String[] args) {
NumberFormat formater = DecimalFormat.getInstance();
formater.setMaximumFractionDigits(4);
formater.setMinimumFractionDigits(4);
System.out.println(Math.round(0.005));
double a = 4;
double b = 7;
System.out.println(formater.format(a/b));
System.out.println(formater.format(1.6757));
System.out.println(formater.format(300));
BigDecimal mData = new BigDecimal(formater.format(2.0/3.0*100)).setScale(2, BigDecimal.ROUND_HALF_UP);
System.out.println(mData.doubleValue());
}
} java
輸出結果: git
0
0.5714
1.6757
300.0000
66.67
.net