Java Program to Convert a Float Value to Absolute Value

In the previous article, we have seen Java Program to Convert an Integer Value to Absolute Value

In this article we are going to see how to find absolute value of a float value using Java programming language.

Java Program to Convert a Float Value to Absolute Value

Before jumping into the program let’s know what is this absolute value first.

Absolute value:

In mathematics, the absolute value or modulus of a real number x is x’s numeric value without regard to its sign.

  • |x| = x if x is positive
  • |x| = −x if x is negative (Here, (-x) is non negative value)
  • |0| = 0.

For example,

The absolute value of 5.5 is 5.5 and

The absolute value of −5.5 is also 5.5

Let’s see different ways to find absolute value of an integer value.

Method-1: Java Program to Convert a Float Value to Absolute Value By Using Math.abs() Method

In Java we have Math.abs() method in java.lang.Math class which can be used to find the absolute value of a number. In this approach we will use this inbuilt Math.abs() method to find the absolute value of integer number.

Approach:

  • Initialize a float variable ‘a’ with a value of -10.11
  • Use Math.abs() method to find the absolute value of a.

Program:

public class Main
{
    public static void main(String[] args) 
    {
        //a float value declared
        float a = -10.11f;
        //finding absolute value using Math.abs(a) and printing the result
        System.out.println("Absolute value of " + a + " is " + Math.abs(a));
    }
}
Output:

Absolute value of -10.11 is 10.11

Method-2: Java Program to Convert a Float Value to Absolute Value By Using If-else Statement

Approach:

  1. Initialize a float variable with a value -10.11.
  2. Check if the value of the variable is negative i.e., less than zero, to get the absolute value, print the negative of the number.
  3. Else if it is positive, print the number as it is.

Program:

public class Main
{
    public static void main(String[] args) 
    {
        //a float value declared
        float a = -10.11f;
        
        //If 'a' value is less than zero then result is -a
        if (a < 0) 
        {
            System.out.println("Absolute value of " + a + " is " + -a);
        }
        //else result is number itself
        else 
        {
            System.out.println("Absolute value of " + a + " is " + a);
        }
    }
}
Output:

Absolute value of -10.11 is 10.11

Method-3: Java Program to Convert a Float Value to Absolute Value By Using Ternary Operator

Approach:

  • Initialize a float variable with initial value as 10.11f.
  • Use the ternary operator to find the absolute value.
  • Here the condition we have to check is whether the given value is greater than 0 or not. So put that before the question mark.
  • If it’s greater than 0 i.e., positive, we’ve to return the value as it is so simply write the variable name in place of expression one.
  • If it’s less than 0 i.e., negative, we’ve to return the negative of that value (negative of negative yields positive value)
  • Print the value.

Program:

public class Main
{
    public static void main(String[] args) 
    {
        //a float value declared
        float a = 10.11f;
        
        System.out.print("Absolute value of " + a);
        //Find absolute value using ternary operator
        a = a > 0 ? a : -a;
        System.out.println(" is " + a);
    }
}
Output:

Absolute value of 10.11 is 10.11

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Related Java Programs: