Operation result: - 1
java.lang.Math class in JDK
- round(): returns rounding, and negative. 5 decimal returns a larger integer, such as - 1.5 returns - 1.
- ceil(): returns the larger value between two integers where the decimal is located, for example, - 1.5 returns - 1.
- tail(): returns the smaller value between two integers where the decimal is located, for example, - 1.5 returns - 2.
Test code:
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));
Print results:
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
- Java self study guide
- Java Interview Questions Summary PC side browse [click here]
- Java knowledge map
- Java interview summary applet browse, scan QR code