Create BigDecimal Values via a long in Java


Let us see how we can create BigDecimal values via a long. Here, we have set long values as a parameter to the BigDecimal constructor.

BigDecimal val1 = BigDecimal.valueOf(289L);
BigDecimal val2 = BigDecimal.valueOf(299L);

We can also perform mathematical operations on it −

val2 = val2.subtract(val1);

The following is an example −

Example

 Live Demo

import java.math.BigDecimal;
public class Demo {
   public static void main(String[] argv) throws Exception {
      BigDecimal val1 = BigDecimal.valueOf(289L);
      BigDecimal val2 = BigDecimal.valueOf(299L);
      System.out.println("Value 1 : "+val1);
      System.out.println("Value 2 : "+val2);
      val2 = val2.subtract(val1);
      System.out.println("Result (Subtraction) = "+val2);
   }
}

Output

Value 1 : 289
Value 2 : 299
Result (Subtraction) = 10

Updated on: 30-Jul-2019

117 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements