Big Numbers in Java Programming
Previous | Home | Next |
Big Numbers are big digit numbers which are not manipulated with the operators such as +,-,*,/ etc. to manipulate these big numbers special classes of java are required .these are classes for manipulating numbers with arbitrarily
long sequence of digits.
The BigInteger class implements arbitrary precision integer arithmetic, and BigDecimal does the same for floating-point numbers.
Static valueof method is used to turn an ordinary number into a big number.
valueof method is defined in java . lang .maths package.as java.math.BigInteger and forfloating numbers as java.math.BigDecimal.
BigInteger s = BigInteger. valueof(450)
we cannot use mathematical operators such as +,-,* and / to do mathematical operation instead we have to use
methods like add, subtract,multiplication,division.for big numbers.
BigInteger s= a.add(b); // this is same like this s=a+b;
BigInteger d = c.multiply(b.add(BigInteger.valueOf(2))); // this is same like c*(b+2)
various methods used in java.math.BigInteger and java.math.BigDecimal are :
BigIntger add(BigInteger and other)
BigInteger subtract(BigInteger and other)
BigInteger multiply(BigInteger and other)
BigInteger divide(BigInteger and other)
BigInteger mod(BigInteger and other)
BigInteger add(BigDecimal and other)
BigInteger subtract(BigDecimal and other)
BigInteger multiply(BigDecimal and other)
BigInteger divide(BigDecimal and other)
BigInteger mod(BigDecimal and other)
import java.math.BigDecimal;
public class AddTwoBigNumbers
{
public static void main(String[] args)
{
BigDecimal num1, num2;
num1 = new BigDecimal(70.00035);
num2 = new BigDecimal(100.0025);
Sum(num1, num2);
}
public static void Sum(BigDecimal val1, BigDecimal val2)
{
BigDecimal sum = val1.add(val2);
System.out.println("Sum of two BigDecimal numbers: "+ sum);
}
Previous | Home | Next |