Java中的Long signum()方法

signum函数也称为符号函数,是一个奇数数学函数,用于提取实数的符号。 爪哇。朗,朗。signum()方法用于获取指定长值的signum函数。对于正值、负值和零,该方法分别返回1、-1和0。

null

语法:

public static int signum(long num)

参数: 该方法接受一个参数 号码 要在其上执行signum操作的长类型。

返回值: 该方法返回指定长值的signum函数。如果指定值为:

  • 如果为负,则该方法返回-1。
  • 零,则该方法返回0。
  • 如果为正,则该方法返回1。

例如:

Input: (Long) 2731766
Output: 1

Input: (Long) -233611 
Output: -1

Input: (Long) 0
Output: 0

下面的程序演示了Java。朗,朗。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

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享