Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does new BigDecimal("0.015").compareTo(new BigDecimal(0.015)) return -1? [duplicate]

Why does new BigDecimal("0.015").compareTo(new BigDecimal(0.015)) return -1? If I expect those two to be equal, is there an alternative way to compare them?

like image 557
Glide Avatar asked Sep 28 '15 20:09

Glide


1 Answers

Due to the imprecise nature of floating point arithmetic, they're not exactly equal

System.out.println(new BigDecimal(0.015));

displays

0.01499999999999999944488848768742172978818416595458984375
like image 93
Reimeus Avatar answered Oct 11 '22 18:10

Reimeus