How to Return Absolute Value of a Number in Java


In this example, we may learn how to find absolute value of any double, float, long or java int numberĀ using abs method of Math class.

Source Code

package com.beginner.examples;

public class AbsoluteExample {

    public static void main(String[] args){
    	//return absolute value of a number.
    	System.out.println("Absolute value of " + (-10) + " is :" + Math.abs(-10));
        System.out.println("Absolute value of " + (10.1) + " is :" + Math.abs(10.1));
    }
}

Output:

Absolute value of -10 is :10
Absolute value of 10.1 is :10.1
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments