关于Math.round()方法
先上结论:
1.参数的小数点后第一位<5,运算结果为参数整数部分。
2.参数的小数点后第一位>5,运算结果为参数整数部分绝对值+1,符号(+ or -)不变。
3.参数的小数点后第一位=5,整数运算结果为整数部分+1,负数运算结果为整数部分。
public class MathTest { public static void main(String[] args) { System.out.println("小数点后第一位=5:"); System.out.println("正数:Math.round(11.5)="+Math.round(11.5)); System.out.println("负数:Math.round(-11.5)="+Math.round(-11.5)); System.out.println(); System.out.println("小数点后第一位<5:"); System.out.println("正数:Math.round(11.46)="+Math.round(11.46)); System.out.println("负数:Math.round(-11.46)="+Math.round(-11.46)); System.out.println(); System.out.println("小数点后第一位>5:"); System.out.println("正数:Math.round(11.68)="+Math.round(11.68)); System.out.println("负数:Math.round(-11.68)="+Math.round(-11.68)); } }
运算结果:


浙公网安备 33010602011771号