Search code examples
javaandroidmathbigdecimal

How do You Find the Number of Places After the Decimal Point with BigDecimal?


I'm trying to use BigDecimals to calculate something and then round to 5 decimal places if it has more than that. How can I do this?

Would scale() work?


Solution

  • Did you already crawl through JavaDoc, especially the function precision()?

    And this here might be a direct solution which makes it needless to check the precision first:

    yournumber.round(new MathContext(5, HALF_UP));
    

    And after that use stripTrailingZeros() (thx to @GregKopff)