符號函數也稱為符號函數,是一種奇數函數,可提取實數的符號。
java.lang.Long.signum()方法用於獲取指定的long值的signum函數。對於正值,負值和零,該方法分別返回1,-1和0。
用法:
public static int signum(long num)
參數:該方法接受一個長類型的參數num,對其執行符號運算。
返回值:該方法返回指定的long值的signum函數。如果指定的值是:
- 負數,該方法返回-1。
- 零,該方法返回0。
- 正數,該方法返回1。
例子:
Input:(Long) 2731766 Output:1 Input:(Long) -233611 Output:-1 Input:(Long) 0 Output:0
以下程序說明了Java.lang.Long.signum()方法:
程序1:
// Java program to illustrate the
// Java.lang.Long.signum() Method
import java.lang.*;
public class Geeks {
public static void main(String[] args)
{
// It will return 1 as long value is greater than 1
System.out.println(Long.signum(36565531));
// It will return -1 as long value is less than 1
System.out.println(Long.signum(-628127));
// Returns 0 as long value is equal to 0
System.out.println(Long.signum(0));
}
}
輸出:
1 -1 0
程序2:用於十進製值和字符串。
// Java program to illustrate the
// Java.lang.Long.signum() Method
import java.lang.*;
public class Geeks {
public static void main(String[] args)
{
// It will return compile time error
System.out.println(Long.signum(36565.531));
// It will return compile time error
System.out.println(Long.signum("628127"));
}
}
輸出:
prog.java:10:error:incompatible types:possible lossy conversion from double to long System.out.println(Long.signum(36565.531)); ^ prog.java:13:error:incompatible types:String cannot be converted to long System.out.println(Long.signum("628127")); ^ Note:Some messages have been simplified; recompile with -Xdiags:verbose to get full output 2 errors
相關用法
- Java BigInteger signum()用法及代碼示例
- Java Integer signum()用法及代碼示例
- Java BigDecimal signum()用法及代碼示例
- Java signum()用法及代碼示例
- Java StrictMath signum()用法及代碼示例
- Java LongMath.divide(long, long, RoundingMode)用法及代碼示例
- Java LongMath.checkedAdd(long a, long b)用法及代碼示例
- Java Longs.checkedSubtract(long a, long b)用法及代碼示例
- Java Longs.indexOf(long[] array, long target)用法及代碼示例
- Java Longs.indexOf(long[] array, long[] target)用法及代碼示例
- Java Duration ofSeconds(long, long)用法及代碼示例
- Java Math addExact(long x, long y)用法及代碼示例
- Java Math subtractExact(long x, long y)用法及代碼示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代碼示例
- Java Java lang.Long.lowestOneBit()用法及代碼示例
注:本文由純淨天空篩選整理自ankita_chowrasia大神的英文原創作品 Long signum() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。