Java Program to create a BigDecimal from a string type value

Java 8Object Oriented ProgrammingProgramming

Let us first create a BigDecimal type with string value −

BigDecimal one = new BigDecimal("562537627.8787867");

Now, create a BigDecimal with long −

BigDecimal two = BigDecimal.valueOf(562L);

After that use add() with the aug end value i.e. the value in “two” object −

one = one.add(two);

Example

 Live Demo

import java.math.BigDecimal;
public class Demo {
   public static void main(String[] argv) throws Exception {
      BigDecimal one = new BigDecimal("562537627.8787867");
      BigDecimal two = BigDecimal.valueOf(562L);
      one = one.add(two);
      System.out.println(one);
   }
}

Output

562538189.8787867
raja
Published on 25-Apr-2019 09:54:27
Advertisements