Java BigDecimal : How to fortmat BigDecimal -


i'm using bigdecimal counting big real number. although have try 2 method : bigdecimal.tostring() or bigdecimal.striptrailingzeros().tostring(), still not sasitfy requirement.

for example if use striptrailingzeros: 4.3000 becomes 4.3 4.0 becomes 4.0 not 4. both above methods cannot sastisty conditions. so, question : how done in java ?

thanks :)

you can use decimalformat follows:

bigdecimal = new bigdecimal("4.3000"); bigdecimal b = new bigdecimal("4.0");  decimalformat f = new decimalformat("#.#"); f.setdecimalseparatoralwaysshown(false) f.setmaximumfractiondigits(340);  system.out.println(f.format(a)); system.out.println(f.format(b)); 

which prints

4.3 4 

as bhashit pointed out, default number of fractional digits 3, can set maximum of 340. wasn't aware of behaviour of decimalformat. means if need more 340 fractional digits, you'll have manipulate string given tostring() yourself.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -