Java : NumberFormat with Examples
NumberFormat (Java SE 18 & JDK 18) API Examples.
You will find code examples on most NumberFormat methods.
Summary
System.out.println(Locale.getDefault().toLanguageTag()); // en-US
final var format = NumberFormat.getNumberInstance();
System.out.println(format.format(1234000)); // 1,234,000
System.out.println(format.format(-56789)); // -56,789
System.out.println(format.format(0.123456)); // 0.123
final var number = 1234;
final var format1 = NumberFormat.getCurrencyInstance(Locale.US);
System.out.println(format1.format(number)); // $1,234.00
final var format2 = NumberFormat.getCurrencyInstance(Locale.JAPAN);
System.out.println(format2.format(number)); // ¥1,234
final var number = 10000000;
final var format1 = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.LONG);
System.out.println(format1.format(number)); // 10 million
final var format2 = NumberFormat.getCompactNumberInstance(Locale.JAPAN, NumberFormat.Style.SHORT);
System.out.println(format2.format(number)); // 1000万
Fields
static final int FRACTION_FIELD
Please see format(double number, StringBuffer toAppendTo, FieldPosition pos).
static final int INTEGER_FIELD
Please see format(double number, StringBuffer toAppendTo, FieldPosition pos).
Constructors
NumberFormat ()
I think it's rare to create a subclass of NumberFormat. Therefore, the code example is omitted.
Methods
Object clone ()
final var number = 1.234;
final var format = NumberFormat.getNumberInstance();
format.setMaximumFractionDigits(2);
System.out.println(format.format(number)); // 1.23
if (format.clone() instanceof NumberFormat cloned) {
System.out.println(cloned.format(number)); // 1.23
System.out.println(cloned.equals(format)); // true
}
boolean equals (Object obj)
final var format1 = NumberFormat.getNumberInstance();
final var format2 = NumberFormat.getCurrencyInstance();
System.out.println(format1.equals(format2)); // false
final var format1 = NumberFormat.getNumberInstance();
final var format2 = NumberFormat.getNumberInstance();
System.out.println(format1.equals(format2)); // true
format1.setMaximumFractionDigits(2);
System.out.println(format1.equals(format2)); // false
final String format (double number)
System.out.println(Locale.getDefault().toLanguageTag()); // en-US
final var number = 1.234;
final var format = NumberFormat.getNumberInstance();
System.out.println(format.format(number)); // 1.234
format.setMaximumFractionDigits(2);
System.out.println(format.format(number)); // 1.23
format.setMinimumIntegerDigits(3);
System.out.println(format.format(number)); // 001.23
abstract StringBuffer format (double number, StringBuffer toAppendTo, FieldPosition pos)
final var number = 123.456;
final var format = NumberFormat.getNumberInstance(Locale.US);
System.out.println(format.format(number)); // 123.456
{
final var sb = new StringBuffer("num = ");
System.out.println(sb); // num =
final var pos = new FieldPosition(NumberFormat.INTEGER_FIELD);
final var ret = format.format(number, sb, pos);
System.out.println(ret); // num = 123.456
System.out.println(pos.getBeginIndex()); // 6
System.out.println(pos.getEndIndex()); // 9
}
{
final var sb = new StringBuffer("num = ");
System.out.println(sb); // num =
final var pos = new FieldPosition(NumberFormat.FRACTION_FIELD);
final var ret = format.format(number, sb, pos);
System.out.println(ret); // num = 123.456
System.out.println(pos.getBeginIndex()); // 10
System.out.println(pos.getEndIndex()); // 13
}
final String format (long number)
System.out.println(Locale.getDefault().toLanguageTag()); // en-US
final var number = 1234L;
final var format1 = NumberFormat.getCurrencyInstance();
System.out.println(format1.format(number)); // $1,234.00
final var format2 = NumberFormat.getCurrencyInstance(Locale.JAPAN);
System.out.println(format2.format(number)); // ¥1,234
abstract StringBuffer format (long number, StringBuffer toAppendTo, FieldPosition pos)
final var format = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
format.setMinimumFractionDigits(2);
final var number = 12340L;
System.out.println(format.format(number)); // 12.34K
{
final var sb = new StringBuffer("num = ");
System.out.println(sb); // num =
final var pos = new FieldPosition(NumberFormat.INTEGER_FIELD);
final var ret = format.format(number, sb, pos);
System.out.println(ret); // num = 12.34K
System.out.println(pos.getBeginIndex()); // 6
System.out.println(pos.getEndIndex()); // 8
}
{
final var sb = new StringBuffer("num = ");
System.out.println(sb); // num =
final var pos = new FieldPosition(NumberFormat.FRACTION_FIELD);
final var ret = format.format(number, sb, pos);
System.out.println(ret); // num = 12.34K
System.out.println(pos.getBeginIndex()); // 9
System.out.println(pos.getEndIndex()); // 11
}
StringBuffer format (Object number, StringBuffer toAppendTo, FieldPosition pos)
final var format = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
format.setMinimumFractionDigits(2);
final var number = new BigInteger("12340000000000");
System.out.println(format.format(number)); // 12.34T
{
final var sb = new StringBuffer("num = ");
System.out.println(sb); // num =
final var pos = new FieldPosition(NumberFormat.INTEGER_FIELD);
final var ret = format.format(number, sb, pos);
System.out.println(ret); // num = 12.34T
System.out.println(pos.getBeginIndex()); // 6
System.out.println(pos.getEndIndex()); // 8
}
{
final var sb = new StringBuffer("num = ");
System.out.println(sb); // num =
final var pos = new FieldPosition(NumberFormat.FRACTION_FIELD);
final var ret = format.format(number, sb, pos);
System.out.println(ret); // num = 12.34T
System.out.println(pos.getBeginIndex()); // 9
System.out.println(pos.getEndIndex()); // 11
}
static Locale[] getAvailableLocales ()
final var locales = NumberFormat.getAvailableLocales();
for (final var locale : locales) {
System.out.println(locale.toLanguageTag());
}
// Result
// ↓
//und
//he
//th-Thai-TH
//nds
//tk-Latn-TM
//ti-ET
//ta-SG
//lv
//en-NU
//...
static NumberFormat getCompactNumberInstance ()
System.out.println(Locale.getDefault().toLanguageTag()); // en-US
final var number = 10000000;
final var format1 = NumberFormat.getCompactNumberInstance();
System.out.println(format1.format(number)); // 10M
final var format2 = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
System.out.println(format2.format(number)); // 10M
final var format3 = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.LONG);
System.out.println(format3.format(number)); // 10 million
final var format4 = NumberFormat.getCompactNumberInstance(Locale.JAPAN, NumberFormat.Style.SHORT);
System.out.println(format4.format(number)); // 1000万
static NumberFormat getCompactNumberInstance (Locale locale, NumberFormat.Style formatStyle)
Please see getCompactNumberInstance().
Currency getCurrency ()
System.out.println(Locale.getDefault().toLanguageTag()); // en-US
final var number = 1234;
final var format1 = NumberFormat.getCurrencyInstance();
System.out.println(format1.format(number)); // $1,234.00
System.out.println(format1.getCurrency()); // USD
System.out.println(format1.getMinimumFractionDigits()); // 2
final var format2 = NumberFormat.getCurrencyInstance(Locale.JAPAN);
System.out.println(format2.format(number)); // ¥1,234
System.out.println(format2.getCurrency()); // JPY
System.out.println(format2.getMinimumFractionDigits()); // 0
static final NumberFormat getCurrencyInstance ()
Please see getCurrency().
static NumberFormat getCurrencyInstance (Locale inLocale)
Please see getCurrency().
static final NumberFormat getInstance ()
This method is equivalent to getNumberInstance().
static NumberFormat getInstance (Locale inLocale)
This method is equivalent to getNumberInstance(inLocale).
static final NumberFormat getIntegerInstance ()
System.out.println(Locale.getDefault().toLanguageTag()); // en-US
final var format1 = NumberFormat.getIntegerInstance();
System.out.println(format1.getRoundingMode()); // HALF_EVEN
System.out.println(format1.isParseIntegerOnly()); // true
System.out.println(format1.format(12345678)); // 12,345,678
System.out.println(format1.format(9.9)); // 10
final var format2 = NumberFormat.getIntegerInstance(Locale.GERMANY);
System.out.println(format2.format(12345678)); // 12.345.678
System.out.println(format2.format(9.9)); // 10
static NumberFormat getIntegerInstance (Locale inLocale)
Please see getIntegerInstance().
int getMaximumFractionDigits ()
System.out.println(Locale.getDefault().toLanguageTag()); // en-US
final var format = NumberFormat.getNumberInstance();
System.out.println(format.getMinimumFractionDigits()); // 0
System.out.println(format.getMaximumFractionDigits()); // 3
System.out.println(format.format(0.01234)); // 0.012
System.out.println(format.format(456.0)); // 456
format.setMinimumFractionDigits(1);
format.setMaximumFractionDigits(4);
System.out.println(format.getMinimumFractionDigits()); // 1
System.out.println(format.getMaximumFractionDigits()); // 4
System.out.println(format.format(0.01234)); // 0.0123
System.out.println(format.format(456.0)); // 456.0
int getMaximumIntegerDigits ()
System.out.println(Locale.getDefault().toLanguageTag()); // en-US
final var format = NumberFormat.getNumberInstance();
System.out.println(format.getMinimumIntegerDigits()); // 1
System.out.println(format.getMaximumIntegerDigits()); // 2147483647
System.out.println(format.format(1234567890)); // 1,234,567,890
System.out.println(format.format(1)); // 1
format.setMinimumIntegerDigits(3);
format.setMaximumIntegerDigits(8);
System.out.println(format.getMinimumIntegerDigits()); // 3
System.out.println(format.getMaximumIntegerDigits()); // 8
System.out.println(format.format(1234567890)); // 34,567,890
System.out.println(format.format(1)); // 001
int getMinimumFractionDigits ()
Please see getMaximumFractionDigits().
int getMinimumIntegerDigits ()
Please see getMaximumIntegerDigits().
static final NumberFormat getNumberInstance ()
System.out.println(Locale.getDefault().toLanguageTag()); // en-US
final var format1 = NumberFormat.getNumberInstance();
System.out.println(format1.format(12345678)); // 12,345,678
System.out.println(format1.format(0.123)); // 0.123
final var format2 = NumberFormat.getNumberInstance(Locale.GERMANY);
System.out.println(format2.format(12345678)); // 12.345.678
System.out.println(format2.format(0.123)); // 0,123
static NumberFormat getNumberInstance (Locale inLocale)
Please see getNumberInstance().
static final NumberFormat getPercentInstance ()
System.out.println(Locale.getDefault().toLanguageTag()); // en-US
final var format1 = NumberFormat.getPercentInstance();
System.out.println(format1.format(0.75)); // 75%
final var format2 = NumberFormat.getPercentInstance(Locale.FRANCE);
System.out.println(format2.format(0.75)); // 75 %
static NumberFormat getPercentInstance (Locale inLocale)
Please see getPercentInstance().
RoundingMode getRoundingMode ()
final var format = NumberFormat.getNumberInstance(Locale.US);
System.out.println(format.getMaximumFractionDigits()); // 3
System.out.println(format.getRoundingMode()); // HALF_EVEN
System.out.println(format.format(0.1114)); // 0.111
System.out.println(format.format(0.1115)); // 0.112
System.out.println(format.format(0.1116)); // 0.112
format.setRoundingMode(RoundingMode.UP);
System.out.println(format.getRoundingMode()); // UP
System.out.println(format.format(0.1114)); // 0.112
System.out.println(format.format(0.1115)); // 0.112
System.out.println(format.format(0.1116)); // 0.112
format.setRoundingMode(RoundingMode.DOWN);
System.out.println(format.getRoundingMode()); // DOWN
System.out.println(format.format(0.1114)); // 0.111
System.out.println(format.format(0.1115)); // 0.111
System.out.println(format.format(0.1116)); // 0.111
int hashCode ()
final var format1 = NumberFormat.getNumberInstance(Locale.US);
System.out.println(format1.hashCode()); // 423132
final var format2 = NumberFormat.getNumberInstance(Locale.JAPAN);
System.out.println(format2.hashCode()); // 423132
final var format3 = NumberFormat.getCurrencyInstance(Locale.US);
System.out.println(format3.hashCode()); // 423168
boolean isGroupingUsed ()
System.out.println(Locale.getDefault().toLanguageTag()); // en-US
final var format = NumberFormat.getNumberInstance();
System.out.println(format.isGroupingUsed()); // true
System.out.println(format.format(1234567)); // 1,234,567
format.setGroupingUsed(false);
System.out.println(format.isGroupingUsed()); // false
System.out.println(format.format(1234567)); // 1234567
boolean isParseIntegerOnly ()
System.out.println(Locale.getDefault().toLanguageTag()); // en-US
final var format = NumberFormat.getNumberInstance();
System.out.println(format.isParseIntegerOnly()); // false
final var ret1 = format.parse("123");
System.out.println(ret1); // 123
final var ret2 = format.parse("4.567");
System.out.println(ret2); // 4.567
format.setParseIntegerOnly(true);
System.out.println(format.isParseIntegerOnly()); // true
final var ret3 = format.parse("4.567");
System.out.println(ret3); // 4
Number parse (String source)
System.out.println(Locale.getDefault().toLanguageTag()); // en-US
final var format = NumberFormat.getNumberInstance();
final var ret1 = format.parse("123,000");
System.out.println(ret1); // 123000
System.out.println(ret1.getClass().getSimpleName()); // Long
final var ret2 = format.parse("0.123");
System.out.println(ret2); // 0.123
System.out.println(ret2.getClass().getSimpleName()); // Double
final var format = NumberFormat.getPercentInstance();
final var ret = format.parse("75%");
System.out.println(ret); // 0.75
System.out.println(ret.getClass().getSimpleName()); // Double
final var format = NumberFormat.getCompactNumberInstance();
final var ret = format.parse("100K");
System.out.println(ret); // 100000
System.out.println(ret.getClass().getSimpleName()); // Long
abstract Number parse (String source, ParsePosition parsePosition)
final var format = NumberFormat.getNumberInstance(Locale.US);
final var source = "123 456";
final var pos = new ParsePosition(0);
final var ret1 = format.parse(source, pos);
System.out.println(ret1); // 123
System.out.println(pos.getIndex()); // 3
pos.setIndex(pos.getIndex() + 1);
final var ret2 = format.parse(source, pos);
System.out.println(ret2); // 456
System.out.println(pos.getIndex()); // 7
final var ret3 = format.parse(source, pos);
System.out.println(ret3); // null
final Object parseObject (String source, ParsePosition pos)
final var format = NumberFormat.getNumberInstance(Locale.US);
final var source = "123 456";
final var pos = new ParsePosition(0);
final var ret1 = format.parseObject(source, pos);
System.out.println(ret1); // 123
System.out.println(pos.getIndex()); // 3
pos.setIndex(pos.getIndex() + 1);
final var ret2 = format.parseObject(source, pos);
System.out.println(ret2); // 456
System.out.println(pos.getIndex()); // 7
final var ret3 = format.parseObject(source, pos);
System.out.println(ret3); // null
void setCurrency (Currency currency)
Please see also : getCurrency()
System.out.println(Locale.getDefault().toLanguageTag()); // en-US
final var number = 1234;
final var format = NumberFormat.getCurrencyInstance();
System.out.println(format.format(number)); // $1,234.00
System.out.println(format.getCurrency()); // USD
format.setCurrency(Currency.getInstance(Locale.JAPAN));
format.setMinimumFractionDigits(0);
System.out.println(format.format(number)); // ¥1,234
System.out.println(format.getCurrency()); // JPY
void setGroupingUsed (boolean newValue)
Please see isGroupingUsed().
void setMaximumFractionDigits (int newValue)
Please getMaximumFractionDigits().
void setMaximumIntegerDigits (int newValue)
Please see getMaximumIntegerDigits().
void setMinimumFractionDigits (int newValue)
Please see getMaximumFractionDigits().
void setMinimumIntegerDigits (int newValue)
Please see getMaximumIntegerDigits().
void setParseIntegerOnly (boolean value)
Please see isParseIntegerOnly().
void setRoundingMode (RoundingMode roundingMode)
Please see getRoundingMode().
Methods declared in Format
format, formatToCharacterIterator, parseObject
Please see the link below.
Related posts
Internationalization
Number
- API Examples