专注Java教育14年 全国咨询/投诉热线:444-1124-454
星辉LOGO图
始于2009,口口相传的Java黄埔军校
首页 学习攻略 Java学习 告诉你Java绝对值函数是什么

告诉你Java绝对值函数是什么

更新时间:2022-11-30 10:51:59 来源:星辉 浏览2332次

为了在 Java 中检索绝对值,我们使用abs()函数。

句法

该abs()函数的一般语法是:

public static datatype abs(datatype number)

参数

该函数将要确定其绝对值的参数作为输入。

返回值

该函数返回参数的绝对值。

例子

让我们来看看我们如何使用该abs()函数找到绝对值!

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) );
  }
}

 

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>