In Java, is new a BigDecimal from another bigDecimal.toString() always equals? For example
BigDecimal a = new BigDecimal("1.23");
BigDecimal b = new BigDecimal(a.toString());
System.out.println(a.compareTo(b) == 0); // always true?
I know BigDecimal is immutable, but i want to know if there is any good way to clone
a BigDecimal object?
Yes you can assume so. From the BigDecimal.toString
docs
If that string representation is converted back to a
BigDecimal
using theBigDecimal(String)
constructor, then the original value will be recovered.
However you can safely share the same object because it is immutable
and copying is not required
One simple way would be to add ZERO
BigDecimal a = new BigDecimal("1.23");
BigDecimal b = a.add(BigDecimal.ZERO);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With