Commonly used methods in java.math package
1.Math.floor(): Round down (but the result is a decimal):
public class TestDemo1 {
public static void main ( String[ ] args) {
System. out. println ( Math. floor ( 11.5 ) ) ;
System. out. println ( Math. floor ( 11.6 ) ) ;
System. out. println ( Math. floor ( 11.4 ) ) ;
System. out. println ( Math. floor ( - 11.5 ) ) ;
System. out. println ( Math. floor ( - 11.6 ) ) ;
System. out. println ( Math. floor ( - 11.4 ) ) ;
}
}
2.Math.abs(): absolute value
public class TestDemo1 {
public static void main ( String[ ] args) {
System. out. println ( Math. abs ( 11.5 ) ) ;
System. out. println ( Math. abs ( 11 ) ) ;
System. out. println ( Math. abs ( - 11.5 ) ) ;
System. out. println ( Math. abs ( - 11 ) ) ;
}
}
3.Math.pow(a,b): power function, a to the b power;
public class TestDemo1 {
public static void main ( String[ ] args) {
System. out. println ( Math. pow ( 2 , 3 ) ) ;
System. out. println ( Math. pow ( 2.0 , 3 ) ) ;
System. out. println ( Math. pow ( 2 , 3.0 ) ) ;
System. out. println ( Math. pow ( 2.0 , 3.0 ) ) ;
}
}
4.Math.sqrt(): Arithmetic square root; (the result is double)
public class TestDemo1 {
public static void main ( String[ ] args) {
System. out. println ( Math. sqrt ( 8 ) ) ;
System. out. println ( Math. sqrt ( 8.0 ) ) ;
System. out. println ( Math. sqrt ( 9 ) ) ;
System. out. println ( Math. sqrt ( 9.0 ) ) ;
}
}
5.- Math.max(): maximum value; Math.min(): minimum value;
public class TestDemo1 {
public static void main ( String[ ] args) {
System. out. println ( Math. max ( 2 , 5 ) ) ;
System. out. println ( Math. max ( 2.0 , 5 ) ) ;
System. out. println ( Math. max ( 2 , 5.0 ) ) ;
System. out. println ( Math. max ( 2.0 , 5.0 ) ) ;
}
}
6.Math.round() rounding
Law ; und(1.5) == 2; because round is rounded for positive numbers, for negative numbers: the law is who is close to, such as -11.5, -11.4 is -11 nearest to -11, and -11.6 is close to -12 , Which is -12;
public class TestDemo1 {
public static void main ( String[ ] args) {
System. out. println ( Math. round ( 1.5 ) ) ;
System. out. println ( Math. round ( 1.6 ) ) ;
System. out. println ( Math. round ( 1.4 ) ) ;
System. out. println ( Math. round ( - 1.5 ) ) ;
System. out. println ( Math. round ( - 1.6 ) ) ;
System. out. println ( Math. round ( - 1.7 ) ) ;
}
}