It is troublesome to perform Integer Optimization in Java, especially rounding and rounding.
The following are three integer operations (including truncation, rounding, and rounding) based on some answers on the Internet, which are similar to process-oriented languages (such as C and basic) (but in Java, it is called the class method, "class name. the Calculation of method name (parameter) is a static method of the class ).
Some friends found online that commented out the correct rounding method, but after my experiment, it was an incorrect rounding method.
Testgetint. Java source code
Import Java. math. bigdecimal; import Java. text. decimalformat; public class testgetint {public static void main (string [] ARGs) {double I = 2, j = 2.1, K = 2.5, M = 2.9; system. out. println ("rounds off decimal places to get an integer: math. floor (2) = "+ (INT) math. floor (I); system. out. println ("rounds off decimal places to get an integer: math. floor (2.1) = "+ (INT) math. floor (j); system. out. println ("rounds off decimal places to get an integer: math. floor (2.5) = "+ (INT) math. floor (k); system. out. println ("rounds off decimal places to get an integer: math. floor (2.9) = "+ (INT) math. floor (m);/* The commented Code cannot be correctly rounded to the entire system. out. println ("rounded to integer: math. RINT (2) = "+ (INT) math. RINT (I); system. out. println ("rounded to integer: math. RINT (2.1) = "+ (INT) math. RINT (j); system. out. println ("rounded to integer: math. RINT (2.5) = "+ (INT) math. RINT (k); system. out. println ("rounded to integer: math. RINT (2.9) = "+ (INT) math. RINT (m); system. out. println ("rounded to integer: (2) =" + new decimalformat ("0 ″). format (I); system. out. println ("rounded to rounded up: (2.1) =" + new decimalformat ("0 ″). format (I); system. out. println ("rounded to rounded up: (2.5) =" + new decimalformat ("0 ″). format (I); system. out. println ("rounded to rounded up: (2.9) =" + new decimalformat ("0 ″). format (I); */system. out. println ("rounded to integer: (2) =" + new bigdecimal ("2 ″). setscale (0, bigdecimal. round_half_up); system. out. println ("rounded to an integer: (2.1) =" + new bigdecimal ("2.1 ″). setscale (0, bigdecimal. round_half_up); system. out. println ("rounded to an integer: (2.5) =" + new bigdecimal ("2.5 ″). setscale (0, bigdecimal. round_half_up); system. out. println ("rounded to an integer: (2.9) =" + new bigdecimal ("2.9 ″). setscale (0, bigdecimal. round_half_up); system. out. println ("consolidation: math. ceil (2) = "+ (INT) math. ceil (I); system. out. println ("consolidation: math. ceil (2.1) = "+ (INT) math. ceil (j); system. out. println ("consolidation: math. ceil (2.5) = "+ (INT) math. ceil (k); system. out. println ("consolidation: math. ceil (2.9) = "+ (INT) math. ceil (m); system. out. println ("rounds off decimal places to get an integer: math. floor (-2) = "+ (INT) math. floor (-I); system. out. println ("rounds off decimal places to get an integer: math. floor (-2.1) = "+ (INT) math. floor (-j); system. out. println ("rounds off decimal places to get an integer: math. floor (-2.5) = "+ (INT) math. floor (-k); system. out. println ("rounds off decimal places to get an integer: math. floor (-2.9) = "+ (INT) math. floor (-M); system. out. println ("rounded to integer: (-2) =" + new bigdecimal ("-2 ″). setscale (0, bigdecimal. round_half_up); system. out. println ("rounded to an integer: (-2.1) =" + new bigdecimal ("-2.1 ″). setscale (0, bigdecimal. round_half_up); system. out. println ("rounded to an integer: (-2.5) =" + new bigdecimal ("-2.5 ″). setscale (0, bigdecimal. round_half_up); system. out. println ("rounded to an integer: (-2.9) =" + new bigdecimal ("-2.9 ″). setscale (0, bigdecimal. round_half_up); system. out. println ("consolidation: math. ceil (-2) = "+ (INT) math. ceil (-I); system. out. println ("consolidation: math. ceil (-2.1) = "+ (INT) math. ceil (-j); system. out. println ("consolidation: math. ceil (-2.5) = "+ (INT) math. ceil (-k); system. out. println ("consolidation: math. ceil (-2.9) = "+ (INT) math. ceil (-m ));}}
The methods used in the above Code have not been systematically studied and verified. If any of your friends finds a problem, please correct it. Thank you.
From: http://www.learndiary.com/2006/12/java%E8%AF%AD%E8%A8%80%E4%B8%AD%E7%9A%84%E5%8F%96%E6%95%B4%E8%BF%90%E7% AE %97%E5%8C%85%E6%8B%AC%E6%88%AA%E5%B0%BE%E5%8F%96%E6%95%B4%E5%9B%9B%E8%88%8D%E4%BA%94%E5%85%A5%E5%87%91%E6%95%B4%EF%BF%BD/