Math.round(-1.5) 等于多少

版权声明:转载请注名出处 https://blog.csdn.net/meism5/article/details/89307441

Math.round(-1.5) 等于多少


-1。

扩展JDK中的java.lang.Math类
round:返回四舍五入,负.5小数返回较大整数,如-1.5返回-1。
ceil:返回小数所在两整数间的较大值,如-1.5返回-1。
tail:返回小数所在两整数间的较小值,如-1.5返回-2。

实验代码

        System.out.println("Math.round(1.4)=" + Math.round(1.4));
		System.out.println("Math.round(-1.4)=" + Math.round(-1.4));
		System.out.println("Math.round(1.5)=" + Math.round(1.5));
		System.out.println("Math.round(-1.5)=" + Math.round(-1.5));
		System.out.println("Math.round(1.6)=" + Math.round(1.6));
		System.out.println("Math.round(-1.6)=" + Math.round(-1.6));
		System.out.println();
		
		System.out.println("Math.ceil(1.4)=" + Math.ceil(1.4));
		System.out.println("Math.ceil(-1.4)=" + Math.ceil(-1.4));
		System.out.println("Math.ceil(1.5)=" + Math.ceil(1.5));
		System.out.println("Math.ceil(-1.5)=" + Math.ceil(-1.5));
		System.out.println("Math.ceil(1.6)=" + Math.ceil(1.6));
		System.out.println("Math.ceil(-1.6)=" + Math.ceil(-1.6));
		System.out.println();
		
		System.out.println("Math.floor(1.4)=" + Math.floor(1.4));
		System.out.println("Math.floor(-1.4)=" + Math.floor(-1.4));
		System.out.println("Math.floor(1.5)=" + Math.floor(1.5));
		System.out.println("Math.floor(-1.5)=" + Math.floor(-1.5));
		System.out.println("Math.floor(1.6)=" + Math.floor(1.6));
		System.out.println("Math.floor(-1.6)=" + Math.floor(-1.6));

打印

Math.round(1.4)=1
Math.round(-1.4)=-1
Math.round(1.5)=2
Math.round(-1.5)=-1
Math.round(1.6)=2
Math.round(-1.6)=-2

Math.ceil(1.4)=2.0
Math.ceil(-1.4)=-1.0
Math.ceil(1.5)=2.0
Math.ceil(-1.5)=-1.0
Math.ceil(1.6)=2.0
Math.ceil(-1.6)=-1.0

Math.floor(1.4)=1.0
Math.floor(-1.4)=-2.0
Math.floor(1.5)=1.0
Math.floor(-1.5)=-2.0
Math.floor(1.6)=1.0
Math.floor(-1.6)=-2.0
 

猜你喜欢

转载自blog.csdn.net/meism5/article/details/89307441