Search code examples
javastringbigdecimal

BigDecimal to string conversion adds a zero before decimal point


In my code, if I convert a string, for example

String str = ".12";
String str2 = "0.12"

to Bigdecimal, both gives me the output 0.12. My output requires me not to change the notation - is there a way I can have BigDecimal conversion from string in this case not add 0 to it for .12? I am okay when it keeps 0.12 to 0.12.

I just want the Bigdecimal to preserve the string input exactly the way it is, no adding of zeroes, no removing of zeroes.

I am doing this:

Bigdecimal bd = new Bigdecimal(str);
bd.toString() gives me 0.12

Solution

  • As you can read here the dot devides the IntegerPart from the FractionPart. So in your case the 0 is added as the IntegerPart because u only pass a fractionPart