When you use stripTrailingZeros for number ending with zero like 10, 50, etc. BigDecimal.toString()
method decided to use scientific notation ( instead 50 prints 5E+1) Here is example code tested with java 17:
public static void main(String[] asd)
{
// prints: "with stripTrailingZeros: 5E+1"
System.out.println("with stripTrailingZeros: " + new BigDecimal("50").stripTrailingZeros());
// prints: "without stripTrailingZeros: 50"
System.out.println("without stripTrailingZeros: " + new BigDecimal("50").toString());
}
I know both are correct but I'm wondering why not both return "50"?
This inconsistency is annoying because it propagates all over the application and in my case leads to inconsistency in my JSON API which is client facing (read by humans).
I know that I can fix this by using BigDecimal.toPlainString()
but it feels wrong to force the serializer (in my case Jackson) to use this method with extra configuration just for this case.