Sunday, May 3, 2020

Double vs BigDecimal ?

  
package com.nagaraju.explore;

import java.math.BigDecimal;

public class BigDecimalExplore {

 public static void main(String[] args) {

  double a = 0.02;
     double b = 0.03;
     double c = b - a;
     System.out.println("Double:-"+c);

     BigDecimal _a = new BigDecimal("0.02");
     BigDecimal _b = new BigDecimal("0.03");
     BigDecimal _c = _b.subtract(_a);
     System.out.println("BigDecimal:-"+_c);
     
 }

}

   
  
   
   Output:
   Double:-0.009999999999999998
   BigDecimal:-0.01
     
  
<[> When we Observe the result, we came to know that BigDecimal is giving the exact value.

No comments:

Post a Comment

Recent Post

Databricks Delta table merge Example

here's some sample code that demonstrates a merge operation on a Delta table using PySpark:   from pyspark.sql import SparkSession # cre...