更新時間:2022-11-30 10:51:59 來源:動力節(jié)點 瀏覽3319次
為了在 Java 中檢索絕對值,我們使用abs()函數(shù)。
該abs()函數(shù)的一般語法是:
public static datatype abs(datatype number)
該函數(shù)將要確定其絕對值的參數(shù)作為輸入。
該函數(shù)返回參數(shù)的絕對值。
讓我們來看看我們?nèi)绾问褂迷揳bs()函數(shù)找到絕對值!
class HelloWorld {
public static void main( String args[] ) {
/* Converting Integer values */
int x = 123;
int y = -789;
System.out.printf( "Absolute Value of x: %d \n", Math.abs(x) );
System.out.printf( "Absolute Value of y: %d \n", Math.abs(y) );
/* Converting Floating Point values */
float a = 1.23f;
float b = -7.9f;
System.out.printf( "Absolute Value of a: %f \n", Math.abs(a) );
System.out.printf( "Absolute Value of b: %f \n", Math.abs(b) );
}
}
相關(guān)閱讀