logo

שיטת Java Math.round()

ה java.lang.Math.round() משמש לעגל את המספרים העשרוניים לערך הקרוב ביותר. שיטה זו משמשת להחזרת הארוך הקרוב ביותר לטיעון, עם קשרים מעוגלים לאינסוף חיובי.

תחביר

 public static int round(float x) public static long round(double x) 

פָּרָמֶטֶר

 x= It is a floating-point value to be rounded to an integer 

לַחֲזוֹר

 This method returns the value of the argument rounded to the nearest int value. 
  • אם הארגומנט הוא מספר חיובי או שלילי, שיטה זו תחזיר את הערך הקרוב ביותר.
  • אם הטיעון אינו מספר (NaN) , שיטה זו תחזור אֶפֶס .
  • אם הטיעון הוא אינסוף חיובי או כל ערך קטן או שווה לערך של מספר שלם.MIN_VALUE , שיטה זו תחזור מספר שלם.MIN_VALUE .
  • אם הטיעון הוא אינסוף שלילי או כל ערך קטן או שווה לערך של ארוך.MAX_VALUE , שיטה זו תחזור ארוך.MAX_VALUE .

דוגמה 1

 public class RoundExample1 { public static void main(String[] args) { double x = 79.52; // find the closest int for the double System.out.println(Math.round(x)); } } 
בדוק את זה עכשיו

תְפוּקָה:

פגז בורן-שוב
 80 

דוגמה 2

 public class RoundExample2 { public static void main(String[] args) { double x = -83.76; // find the closest int for the double System.out.println(Math.round(x)); } } 
בדוק את זה עכשיו

תְפוּקָה:

 -84 

דוגמה 3

 public class RoundExample3 { public static void main(String[] args) { double negativeInfinity = Double.NEGATIVE_INFINITY; // Input negative Infinity, Output Long.MAX_VALUE System.out.println(Math.round(negativeInfinity)); } } 
בדוק את זה עכשיו

תְפוּקָה:

 -9223372036854775808 

דוגמה 4

 public class RoundExample4 { public static void main(String[] args) { double x = 1.0/0; // Input positive Infinity, Output Integer.MAX_VALUE System.out.println(Math.round(x)); } } 
בדוק את זה עכשיו

תְפוּקָה:

 9223372036854775807 

דוגמה 5

 public class RoundExample5 { public static void main(String[] args) { double x = 0.0/0; // Input NaN, Output Zero System.out.println(Math.round(x)); } } 
בדוק את זה עכשיו

תְפוּקָה:

 0