How to use the Math.abs() function get the absolute value of a number in Java

1 Answer

0 votes
package javaapplication1;

public class JavaApplication1 {

    public static void main(String[] args) {

        try {

            int n = Math.abs(-1);
            System.out.println(n);

            double d = Math.abs(-3.14);
            System.out.println(d);

            n = Math.abs(1);
            System.out.println(n);

        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }
}

/*
                  
run:
     
1
3.14
1
         
 */

 



answered Dec 15, 2016 by avibootz

Related questions

...