「Java」Math.abs()で絶対値を取得するサンプル
説明
1.public static int abs(int a)
int値の絶対値を返します。引数が負でない場合は引数そのものを返します。
2.public static long abs(long a)
long値の絶対値を返します。引数が負でない場合は引数そのものを返します。
3.public static double abs(double a)
double値の絶対値を返します。引数が負でない場合は引数そのものを返します。
4.public static float abs(float a)
float値の絶対値を返します。引数が負でない場合は引数そのものを返します。
Javaコード
package com.arkgame.study;
public class AbsDemo {
public static void main(String[] args) {
int cftA = -123;
long cftB = -4567899;
double cftC = -51.36;
float cftD = -349.12f;
System.out.println("***実行結果***");
System.out.println("int値の絶対値: " + Math.abs(cftA));
System.out.println("long値の絶対値: " + Math.abs(cftB));
System.out.println("double値の絶対値: " + Math.abs(cftC));
System.out.println("float値の絶対値: " + Math.abs(cftD));
}
}
***実行結果***
int値の絶対値: 123
long値の絶対値: 4567899
double値の絶対値: 51.36
float値の絶対値: 349.12