简体   繁体   中英

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

My percentages get truncated by the default java.text.MessageFormat function, how do you format a percentage without losing precision?

Example:

String expectedResult = "12.5%";
double fraction = 0.125;

String actualResult = MessageFormat.format("{0,number,percent}", fraction);
assert expectedResult.equals(actualResult) : actualResult +" should be formatted as "+expectedResult;

Looks like this:

String actualResult = MessageFormat.format("{0,number,#.##%}", fraction);

... is working.

EDIT: To see how are the #'s and %'s interpreted, see the javadoc of java.text.DecimalFormat.

EDIT 2: And, yes, it is safe for internationalization. The dot in format string is interpreted as a decimal separator, not as a hardcoded dot. :-)

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.

How about

DecimalFormat f = new DecimalFormat( "###.#" );
System.out.println( f.format( 12.5 ) );

The format char '#' does not print a 0 as absent. So 12.5 ->"12.5", 12.0 -> "12", not "12.0". You could of course set up your formatter with eg "###,###.##", the hundreths place will only show up if you need the precision.

You do realize that "expectedResult == actualResult" will always be false, right?

Anyway, the best solution I can find is to set the formatter explicitly. This is the code I tested:

String expectedResult = "12.5%";
double fraction = 0.125;
MessageFormat fmt = new MessageFormat("{0,number,percent}");
NumberFormat nbFmt = NumberFormat.getPercentInstance();
nbFmt.setMaximumFractionDigits(1); // or 2, or however many you need
fmt.setFormatByArgumentIndex(0, nbFmt);
String actualResult = fmt.format(new Object[] {fraction});
assert expectedResult.equals(actualResult) : actualResult +" is getting rounded off";

My solution renders as many fractional digits as scale was set on given Number. Unit tested with many primitive and Number types.

/**
 * Formats the given Number as percentage with necessary precision.
 * This serves as a workaround for {@link NumberFormat#getPercentInstance()} which does not renders fractional
 * digits.
 *
 * @param number
 * @param locale
 *
 * @return
 */
public static String formatPercentFraction(final Number number, final Locale locale)
{
    if (number == null)
        return null;

    // get string representation with dot
    final String strNumber = NumberFormat.getNumberInstance(Locale.US).format(number.doubleValue());
    // create exact BigDecimal and convert to get scale
    final BigDecimal dNumber = new BigDecimal(strNumber).multiply(new BigDecimal(100));

    final NumberFormat percentScaleFormat = NumberFormat.getPercentInstance(locale);
    percentScaleFormat.setMaximumFractionDigits(Math.max(0, dNumber.scale()));

    // convert back for locale percent formatter
    return percentScaleFormat.format(dNumber.multiply(new BigDecimal(0.01)));
}

If internationalization is a concern:

// get locale from somewhere. default is usually a bad idea, especially
// if running in a app server
Locale locale = Locale.getDefault();
NumberFormat fmt = NumberFormat.getPercentInstance(locale);
// the question requires 1 digit for fractional part
// therefore set both, minimum and maximum digit count
fmt.setMinimumFractionDigits(1);
fmt.setMaximumFractionDigits(1);
// set grouping, if you expect large values
// notabene: not all languages use 3 digits per group
fmt.setGroupingUsed(true);
// output the example of the original question
System.out.println(fmt.format(0.125));

The percent number formatter appends a space and the percent sign to the output in the locales I know. I don't know, whether the position of the percent sign is before the digits in other locales (right-to-left locales for instance).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM