signum java_Java Long signum()用法及代码示例

符号函数也称为符号函数,是一种奇数函数,可提取实数的符号。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值