number formatting - Java NumberFormat -


i'm trying use java's numberformat class , getpercentinstance method in program calculate tax. want program display percentage 2 decimal places. now, when tried format decimal percent before, java displayed 0.0625 6%. how make java display decimal or 0.0625 "6.25%"?

code fragment:

numberformat fmt1 = numberformat.getcurrencyinstance(); numberformat fmt2 = numberformat.getpercentinstance();  system.out.print("enter quantity of items purchased: "); quantity = scan.nextint();  system.out.print("enter unit price: "); unitprice = scan.nextdouble();  subtotal = quantity * unitprice; final double tax_rate = .0625; tax = subtotal * tax_rate; totalcost = subtotal + tax;  system.out.println("subtotal: " + fmt1.format(subtotal)); system.out.println("tax: " + fmt1.format(tax) + " @ " + fmt2.format(tax_rate)); system.out.println("total: " + fmt1.format(totalcost)); 

you can set minimum number of fraction digits on numberformat instance using setminimumfractiondigits(int).

for instance:

numberformat f = numberformat.getpercentinstance(); f.setminimumfractiondigits(3); system.out.println(f.format(0.045317d)); 

produces:

4.532% 

Comments