How to use BigDecimal in Java

1 Answer

0 votes
package javaapplication1;
 
import java.math.BigDecimal;

public class JavaApplication1 {
 
    public static void main(String[] args) {
  
        double d = 3.14;
        BigDecimal a = new BigDecimal(d);
        BigDecimal b = new BigDecimal("1");
        BigDecimal c = new BigDecimal("0.0000000001");
       
        BigDecimal x = b.divide(a, 9, BigDecimal.ROUND_FLOOR);
               
        BigDecimal y = b.add(c);
        
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(x);
        System.out.println(y);
    }
}
 
/*
    
run:
    
3.140000000000000124344978758017532527446746826171875
1
1E-10
0.318471337
1.0000000001
    
*/

 



answered Nov 2, 2016 by avibootz

Related questions

1 answer 133 views
133 views asked Jul 24, 2022 by avibootz
2 answers 202 views
202 views asked Jul 25, 2019 by avibootz
2 answers 125 views
1 answer 130 views
1 answer 117 views
1 answer 97 views
97 views asked Jun 3, 2024 by avibootz
1 answer 130 views
...