How do I get the PI value?

The number π is a mathematical constant, the ratio of a circle’s circumference to its diameter, commonly approximated as 3.14159. It has been represented by the Greek letter “π” since the mid-18th century, though it is also sometimes spelled out as “pi” (/paɪ/).

From: Wikipedia

The code snippet below show you how to obtain the PI value in Java. We use the Math.PI static field to get the value of PI.

package org.kodejava.example.math;

public class GetPIExample {
    public static void main(String[] args) {
        // The PI value represented by Math.PI
        System.out.println("PI = " + Math.PI);

        // Using the Math.PI to calculate area of a circle.
        double radius = 8;
        double circleArea = Math.PI * Math.pow(radius, 2);
        System.out.println("Circle Area = " + circleArea);
    }
}

Here is the program output:

PI = 3.141592653589793
Circle Area = 201.06192982974676

How do I do math operation for BigDecimal?

package org.kodejava.example.math;

import java.math.BigDecimal;

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);
        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 create random number?

package org.kodejava.example.lang;

public class RandomExample {
    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 from n to m we can add a n value to the
        // Math.random() as the lowest number and then multiply it with the
        // the highest number. The example below creates random number
        // between 100.0 and 200.0.
        number = 100 + (Math.random() * 100);
        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.670447515721026
Generated number: 5.118459516569203
Generated number: 139.89504952544274
Generated number: 158