Search code examples
javabigdecimal

Extend scale of BigDecimal number


I have a BigDecimal number and i want to add 0 in some case. when i change scale of a BigDecimal it return me only a subNumber of my BigDecimal number

example :

BigDecimal c = new BigDecimal(0.1721112427968000094313954596298853517869092775632121988466426373478102735617940027951431710952381083564411002004385868111); System.out.println(c.setScale(120));

it return :

0.172111242796800012477831387514015659689903259277343750000000000000000000000000000000000000000000000000000000000000000000

how to return a same number with 0 at last of my BigDecimal number ?


Solution

  • You are losing precision because your argument to BigDecimal is being truncated to a double. You can overcome this if you pass in the same argument but as a String (put quotes around it)