java中取long的绝对值_在Java中获取float,int,double和long的绝对值

java.lang.Math类具有abs()方法,可帮助我们查找不同数据类型的绝对值。

浮动绝对值

为了计算float值的绝对值,我们使用java.lang.Math.abs(float a)方法。如果参数'a'为负,则返回'a'的否定。如果参数“ a”为非负数,则返回参数本身。当参数为正零或负零时,结果为正零。如果参数为无穷大,则结果为正无穷大。如果参数为NaN,则结果为NaN。

声明- Math.abs的声明(浮起)函数是如下-public static float abs(float a)

其中a是要返回其绝对值的参数。

int的绝对值

为了计算int值的绝对值,我们使用java.lang.Math.abs(int a)方法。如果参数'a'为负,则返回'a'的否定。如果参数'a'为非负数,则返回参数本身。如果参数“ a”具有值Integer.MIN_VALUE,则返回负值本身。当参数为正零或负零时,结果为正零。

声明- Math.abs的声明(INT一个)功能如下-public static int abs(int a)

其中a是要返回其绝对值的参数。

绝对值的两倍

为了计算double值的绝对值,我们必须使用java.lang.Math.abs(double a)方法。如果参数'a'为负,则返回'a'的否定。如果参数'a'为非负数,则返回参数本身。当参数为正零或负零时,结果为正零。如果参数为无穷大,则结果为正无穷大。如果参数为NaN,则结果为NaN。

声明- Math.abs的声明(双A)功能如下-public static double abs(double a)

其中a是要返回其绝对值的参数。

长的绝对值

为了计算long值的绝对值,我们使用java.lang.Math.abs(long a)方法。如果参数'a'为负,则返回'a'的否定。如果参数“ a”为非负数,则返回参数本身。如果参数“ a”具有值Long.MIN_VALUE,则返回负值本身。当参数为正零或负零时,结果为正零。

声明- Math.abs的声明(长)功能如下-public static long abs(long a)

其中a是要返回其绝对值的参数。

让我们看一个程序,在其中找到float,int,double和long数据类型的绝对值。

示例import java.lang.Math;

public class Example {

public static void main(String[] args) {

// declaring and initialising some integer values

int a = 10;

int b = -9;

// declaring and initialising some float values

float c = 8.11f;

float d = -9.32f;

// declaring and initialising some double values

double x = -100.01d;

double y = 90.344d;

// declaring and initialising some long values

long r = 1234567891223l;

long s = -4567891234554l;

//printing their absolute values

System.out.println("Absolute value of " + a + " = " + Math.abs(a));

System.out.println("Absolute value of " + b + " = " + Math.abs(b));

System.out.println("Absolute value of " + c + " = " + Math.abs(c));

System.out.println("Absolute value of " + d + " = " + Math.abs(d));

System.out.println("Absolute value of " + x + " = " + Math.abs(x));

System.out.println("Absolute value of " + y + " = " + Math.abs(y));

System.out.println("Absolute value of " + r + " = " + Math.abs(r));

System.out.println("Absolute value of " + s + " = " + Math.abs(s));

}

}

输出结果Absolute value of 10 = 10

Absolute value of -9 = 9

Absolute value of 8.11 = 8.11

Absolute value of -9.32 = 9.32

Absolute value of -100.01 = 100.01

Absolute value of 90.344 = 90.344

Absolute value of 1234567891223 = 1234567891223

Absolute value of -4567891234554 = 4567891234554

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值