Java 教程 Java abs() 方法

rolland · 2022-05-24 23:03:21 · 热度: 33

abs() 返回参数的绝对值。参数可以是 int, float, long, double, short, byte类型。

语法

各个类型的方法格式类似如下:

double abs(double d)
float abs(float f)
int abs(int i)
long abs(long lng)

参数

  • 任何原生数据类型。

返回值

返回参数的绝对值。

实例

实例

public class Test{
    public static void main(String args[]){
        Integer a = -8;
        double d = -100;
        float f = -90f;    
                                               
        System.out.println(Math.abs(a));
        System.out.println(Math.abs(d));    
        System.out.println(Math.abs(f));    
    }
}

编译以上程序,输出结果为:

8
100.0
90.0

查看更多 Java Number & Math 类 相关内容

猜你喜欢:
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册