• Post Reply Bookmark Topic Watch Topic
  • New Topic

Decimal Places When Displaying Double in JLabel  RSS feed

 
Marcus Hathaway
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i want to show a decimal placed VAT rate in a JLabel, so the user is aware of the VAT rate they have selected (e.g. display 17.5%). The code i am using to achieve this can be summerised as:

Double dVatR = 17.5;
JLabel label = new JLabel();
NumberFormat nf = NumberFormat.getPercentInstance();
label.setText("" + nf.format(dVatR));

Problem is, this display 1,750% instead of 17.5%. Anyone have any suggestions on how to set the decimal place other than to manulaly perform a mathematical equation (i.e 1750/100) to work it out?

Cheers for any help!
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add the following:

nf.setMinimumFractionDigits(2);
nf.setMaximumFractionDigits(2);
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!