use of com.ibm.icu.text.CompactDecimalFormat in project closure-templates by google.
the class I18NDirectivesRuntime method formatNum.
/**
* Formats a number using ICU4J. Note: If min or max fraction digits is null, the param will be
* ignored.
*/
public static String formatNum(ULocale uLocale, double number, String formatType, String numbersKeyword, @Nullable Integer minFractionDigits, @Nullable Integer maxFractionDigits) {
uLocale = uLocale.setKeywordValue("numbers", numbersKeyword);
NumberFormat numberFormat;
switch(formatType) {
case "decimal":
numberFormat = NumberFormat.getInstance(uLocale);
break;
case "percent":
numberFormat = NumberFormat.getPercentInstance(uLocale);
break;
case "currency":
numberFormat = NumberFormat.getCurrencyInstance(uLocale);
break;
case "scientific":
numberFormat = NumberFormat.getScientificInstance(uLocale);
break;
case "compact_short":
{
CompactDecimalFormat compactNumberFormat = CompactDecimalFormat.getInstance(uLocale, CompactStyle.SHORT);
compactNumberFormat.setMaximumSignificantDigits(3);
numberFormat = compactNumberFormat;
break;
}
case "compact_long":
{
CompactDecimalFormat compactNumberFormat = CompactDecimalFormat.getInstance(uLocale, CompactStyle.LONG);
compactNumberFormat.setMaximumSignificantDigits(3);
numberFormat = compactNumberFormat;
break;
}
default:
throw new IllegalArgumentException("First argument to formatNum must be " + "constant, and one of: 'decimal', 'currency', 'percent', 'scientific', " + "'compact_short', or 'compact_long'.");
}
if (minFractionDigits != null) {
numberFormat.setMinimumFractionDigits(minFractionDigits);
}
if (maxFractionDigits != null) {
numberFormat.setMaximumFractionDigits(maxFractionDigits);
}
return numberFormat.format(number);
}
Aggregations