Search code examples
javastringbigdecimal

How to make a string from a BegDecimal number which contained only the integral part


My question is about of toString() and toPlainString() methods of the BigDecimal dataTypewhich produces the output like

750.0000
150.0000
... etc

My question is how to specify the number of zeros followed after the dot? Is there a way to do it instead of String.replace(".0000", ".00") method?


Solution

  • Apart from decimal format you can also use setScale(2) like this

    new BigDecimal("1.0000").setScale(2)
    

    Also setScale allows you can specify the Rounding Mode