## 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.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 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 get the minimum or maximum value between two numbers?

The code below show you how to use the `Math.min()` and `Math.max()` method call the get the minimum and maximum value between two numbers. As other method in the `Math` class these methods also overloaded to accept many types of primitive data.

``````package org.kodejava.math;

public class GetMinMaxValueExample {
public static void main(String[] args) {

Double value1 = 100.0D;
Double value2 = 200.0D;

double max1 = Math.max(value1, value2);
double min1 = Math.min(value1, value2);

float max2 = Math.max(value1.floatValue(), value2.floatValue());
float min2 = Math.min(value1.floatValue(), value2.floatValue());

int max3 = Math.max(value1.intValue(), value2.intValue());
int min3 = Math.min(value1.intValue(), value2.intValue());

long max4 = Math.max(value1.longValue(), value2.longValue());
long min4 = Math.min(value1.longValue(), value2.longValue());

System.out.println("Max value in double: " + max1);
System.out.println("Min value in double: " + min1);
System.out.println("Max value in float : " + max2);
System.out.println("Min value in float : " + min2);
System.out.println("Max value in int   : " + max3);
System.out.println("Min value in int   : " + min3);
System.out.println("Max value in long  : " + max4);
System.out.println("Min value in long  : " + min4);
}
}
``````

The result of the above program are:

``````Max value in double: 200.0
Min value in double: 100.0
Max value in float : 200.0
Min value in float : 100.0
Max value in int   : 200
Min value in int   : 100
Max value in long  : 200
Min value in long  : 100
``````

## 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));

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
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");

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;

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
``````