Closed rogervila closed 4 years ago
Hi, you're misusing floats. You should never compare them for equality, they're imprecise by design.
Your PR doesn't make much sense: if I call BigDecimal::of(123)->toFloat(2)
, I get the exact same value as if I call BigDecimal::of(123)->toFloat(0)
!
Bottom line: use BigDecimal
to compare your decimal values, only convert to float
if you have a compelling reason to do so!
Ok, I will change the strategy. Thank you for your advice :)
I had to compare 2 float values using BigDecimal class, but I was getting an unexpected result because those floats had no decimals:
With this solution, specifying the amount of decimals will get expected results