beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
    • Learn jQuery
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Get absolute value of float, int, double and long using Math.abs in Java

By Chaitanya Singh | Filed Under: Java.lang.Math

Description

In this tutorial we are gonna see a program to find out the absolute values of float, int, double and long variables in java.
public static double abs(double a): Returns the absolute value of a double value.
public static float abs(float a): Returns the absolute value of a float value.
public static long abs(long a): Returns the absolute value of a long value.
public static int abs(int a): Returns the absolute value of a int value.
Note: The below points are applicable for all the above four variations of abs() method
If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned. Special cases:
If the argument is positive zero or negative zero, the result is positive zero.
If the argument is infinite, the result is positive infinity.
If the argument is NaN, the result is NaN.
In other words, the result is the same as the value of the expression:
Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1). Source.

Example

class AbsOfValues {

   public static void main(String[] args) {

      // variables of type double
      double dvar = 123456.99d;
      double dvar2 = -445.889d;

      // Displaying Absolute values
      System.out.println("Math.abs(" +dvar+ "): " + Math.abs(dvar));
      System.out.println("Math.abs(" +dvar2+ "): " + Math.abs(dvar2));
	      
      // variables of type float
      float fvar = 1256.99f;
      float fvar2 = -45.889f;

      // Displaying Absolute values
      System.out.println("Math.abs(" +fvar+ "): " + Math.abs(fvar));
      System.out.println("Math.abs(" +fvar2+ "): " + Math.abs(fvar2));
	      
      // variables of type long
      long lvar = 12345999L;
      long lvar2 = -475889L;

      // Displaying Absolute values
      System.out.println("Math.abs(" +lvar+ "): " + Math.abs(lvar));
      System.out.println("Math.abs(" +lvar2+ "): " + Math.abs(lvar2));
      
      // variables of type int
      int ivar = 1234;
      int ivar2 = -44;

      // Displaying Absolute values
      System.out.println("Math.abs(" +ivar+ "): " + Math.abs(ivar));
      System.out.println("Math.abs(" +ivar2+ "): " + Math.abs(ivar2));
  }
}

Output:

Math.abs(123456.99): 123456.99
Math.abs(-445.889): 445.889
Math.abs(1256.99): 1256.99
Math.abs(-45.889): 45.889
Math.abs(12345999): 12345999
Math.abs(-475889): 475889
Math.abs(1234): 1234
Math.abs(-44): 44

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap