Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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.
–
–
–
–
–
From the API documetation of stripTrailingZeros()
:
Returns a BigDecimal which is numerically equal to this one but with
any trailing zeros removed from the representation.
What else would you expect that it returns? "5"?
–
–
Thanks to @yshavit for his comment. Focused on my case working with decimals and removing trailing zeros in the decimal part, I totally forget that it will remove zeros from the non-decimal part also. So it is expected from new BigDecimal("50").stripTrailingZeros().toString()
to return "5E+1" (without trailing zeros) instead of "50" (with trailing zero). From stripTrailingZeros()
docs:
For example, stripping the trailing zeros from the BigDecimal value 600.0, which has [BigInteger, scale] components equal to [6000, 1], yields 6E2 with [BigInteger, scale] components equal to [6, -2].
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.