How do I get the absolute value of a number?

The example below show you how to get the absolute value of a number. To get the absolute value or the abs value of a number we use the Math.abs() method call. The Math.abs() method is an overloaded that can accept value in type of double, float, int or long.

package org.kodejava.math;

public class GetAbsoluteValueExample {
    public static void main(String[] args) {
        Double value = -10.0D;

        double abs1 = Math.abs(value);
        System.out.println("Absolute value in double: " + abs1);

        float abs2 = Math.abs(value.floatValue());
        System.out.println("Absolute value in float : " + abs2);

        int abs3 = Math.abs(value.intValue());
        System.out.println("Absolute value in int   : " + abs3);

        long abs4 = Math.abs(value.longValue());
        System.out.println("Absolute value in long  : " + abs4);
    }
}

The code snippet above print the following result:

Absolute value in double: 10.0
Absolute value in float : 10.0
Absolute value in int   : 10
Absolute value in long  : 10

How do I convert BigInteger into another radix number?

In this example you’ll see how we can convert a java.math.BigInteger number into another radix such as binary, octal and hexadecimal.

package org.kodejava.math;

import java.math.BigInteger;

public class BigIntegerConversion {
    public static void main(String[] args) {
        BigInteger number = new BigInteger("2021");
        System.out.println("Number      = " + number);
        System.out.println("Binary      = " + number.toString(2));
        System.out.println("Octal       = " + number.toString(8));
        System.out.println("Hexadecimal = " + number.toString(16));

        number = new BigInteger("FF", 16);
        System.out.println("Number      = " + number);
        System.out.println("Number      = " + number.toString(16));
    }
}

The result of our examples:

Number      = 2021
Binary      = 11111100101
Octal       = 3745
Hexadecimal = 7e5
Number      = 255
Number      = ff

How do I do math operation for BigDecimal?

package org.kodejava.math;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class BigDecimalOperation {
    public static void main(String[] args) {
        BigDecimal decimalA = new BigDecimal("98765432123456789");
        BigDecimal decimalB = new BigDecimal("10");

        decimalA = decimalA.add(decimalB);
        System.out.println("decimalA = " + decimalA);

        decimalA = decimalA.multiply(decimalB);
        System.out.println("decimalA = " + decimalA);

        decimalA = decimalA.subtract(decimalB);
        System.out.println("decimalA = " + decimalA);

        decimalA = decimalA.divide(decimalB, RoundingMode.DOWN);
        System.out.println("decimalA = " + decimalA);

        decimalA = decimalA.pow(2);
        System.out.println("decimalA = " + decimalA);

        decimalA = decimalA.negate();
        System.out.println("decimalA = " + decimalA);
    }
}

Our code snippet results are:

decimalA = 98765432123456799
decimalA = 987654321234567990
decimalA = 987654321234567980
decimalA = 98765432123456798
decimalA = 9754610582533151990855052972412804
decimalA = -9754610582533151990855052972412804

How do I do math operation for BigInteger?

package org.kodejava.math;

import java.math.BigInteger;

public class BigIntegerOperation {
    public static void main(String[] args) {
        BigInteger numberA = new BigInteger("98765432123456789");
        BigInteger numberB = BigInteger.TEN;

        numberA = numberA.add(numberB);
        System.out.println("numberA = " + numberA);

        numberA = numberA.multiply(numberB);
        System.out.println("numberA = " + numberA);

        numberA = numberA.subtract(numberB);
        System.out.println("numberA = " + numberA);

        numberA = numberA.divide(numberB);
        System.out.println("numberA = " + numberA);

        numberA = numberA.mod(numberB);
        System.out.println("numberA = " + numberA);

        numberA = numberA.pow(2);
        System.out.println("numberA = " + numberA);

        numberA = numberA.negate();
        System.out.println("numberA = " + numberA);
    }
}

Our class result are:

numberA = 98765432123456799
numberA = 987654321234567990
numberA = 987654321234567980
numberA = 98765432123456798
numberA = 8
numberA = 64
numberA = -64

How do I create random number?

The java.lang.Math.random() method returns random number between 0.0 and 1.0 including 0.0 but not including 1.0. By multiplying Math.random() result with a number, for example 10 will give us a range of random number between 0.0 and 10.0.

To get a random number between two numbers (n and m) we can use the formula of: n + (Math.random() * (m - n)). Where n is the lowest number (inclusive) and m is the highest number (exclusive).

package org.kodejava.lang;

public class RandomNumberExample {
    public static void main(String[] args) {
        // The Math.random() returns a random number between 0.0 and 1.0 
        // including 0.0 but not including 1.0.
        double number = Math.random();
        System.out.println("Generated number: " + number);

        // By multiplying Math.random() result with a number will give
        // us a range of random number between, for instance 0.0 to 10.0 as
        // shown in the example below.
        number = Math.random() * 10;
        System.out.println("Generated number: " + number);

        // To get a random number between n and m we can use the formula:
        // n + (Math.random() * (m - n)). The example below creates random
        // number between 100.0 and 200.0.
        int n = 100;
        int m = 200;
        number = n + (Math.random() * (m - n));
        System.out.println("Generated number: " + number);

        // Creates an integer random number
        int random = 100 + (int) (Math.random() * 100);
        System.out.println("Generated number: " + random);
    }
}

Here is an example result of our program.

Generated number: 0.024974902600698234
Generated number: 2.271051510086771
Generated number: 147.542543014888
Generated number: 112