Java Program to create Big Decimal Values via a string


Let’s create BigDecimal value with a string

BigDecimal val1 = new BigDecimal("37578975.8768");
BigDecimal val2 = new BigDecimal("62567878.9768");

You can also set the above as −

String str1 = “37578975.8768”;
BigDecimal val1 = new BigDecimal(str1);
String str2 = “62567878.9768”;
BigDecimal val2 = new BigDecimal(str2);

Now, you can also perform operations like −

val1 = val1.add(val2);
System.out.println("Addition Operation = " + val1);

The following is an example −

Example

 Live Demo

import java.math.BigDecimal;
public class Demo {
   public static void main(String[] argv) throws Exception {
      // creating BigDecimal values via a string
      BigDecimal val1 = new BigDecimal("37578975.8768");
      BigDecimal val2 = new BigDecimal("62567878.9768");
      System.out.println("Value 1 : "+val1);
      System.out.println("Value 2 : "+val2);
      val1 = val1.add(val2);
      System.out.println("Addition Operation = " + val1);
      val1 = val1.multiply(val2);
      System.out.println("Multiplication Operation = " + val1);
   }
}

Output

Value 1 : 37578975.8768
Value 2 : 62567878.9768
Addition Operation = 100146854.8536
Multiplication Operation = 6265976294387200.48179648

Updated on: 30-Jul-2019

58 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements