Create BigDecimal Values via a long in Java

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 20-Dec-2018 15:38:18
Advertisements