How do you format a fractional percentage with java.text.MessageFormat

I think the proper way to do it is the following:

NumberFormat percentFormat = NumberFormat.getPercentInstance();
percentFormat.setMaximumFractionDigits(1);
String result = percentFormat.format(0.125);

It also takes internalization into account. For example on my machine with hungarian locale I got “12,5%” as expected. Initializing percentFormat as NumberFormat.getPercentInstance(Locale.US) gives “12.5%” of course.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)