-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigDecimalClass.java
More file actions
40 lines (25 loc) · 894 Bytes
/
BigDecimalClass.java
File metadata and controls
40 lines (25 loc) · 894 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package section3;
import java.math.BigDecimal;
public class BigDecimalClass {
public static void main(String[] args) {
double x = 1.45;
double y = 2.55;
System.out.println(x);
System.out.println(y);
System.out.println(x + y);
System.out.println();
// issue
x = 1.05;
y = 2.55;
System.out.println(x + y); // gives unexpected, wrong output
// for ultra precison make use of BigDecimal class in Java
BigDecimal d1 = new BigDecimal(1.05);
BigDecimal d2 = new BigDecimal(2.55);
System.out.println(d1.add(d2)); // this creates even more issue
// to solve this use the String version of this number, if we do so then
// rounding off will be disabled by default and we'll get the correct result
BigDecimal big1 = new BigDecimal("1.05");
BigDecimal big2 = new BigDecimal("2.55");
System.out.println(big1.add(big2)); // gives correct output now
}
}