Java : Format with Examples
Format (Java SE 18 & JDK 18) API Examples.
You will find code examples on most Format methods.
Summary
final var value = 1234;
final Format format2 = NumberFormat.getCurrencyInstance(Locale.US);
System.out.println(format2.format(value)); // $1,234.00
final Format format1 = NumberFormat.getCurrencyInstance(Locale.JAPAN);
System.out.println(format1.format(value)); // ¥1,234
Constructors
Format ()
I think it's rare to create a subclass of Format. Therefore, the code example is omitted.
Methods
Object clone ()
System.out.println(Locale.getDefault().toLanguageTag()); // en-US
final var value = 1234;
final Format format = NumberFormat.getCurrencyInstance();
System.out.println(format.format(value)); // $1,234.00
if (format.clone() instanceof Format cloned) {
System.out.println(cloned.format(value)); // $1,234.00
}
final String format (Object obj)
System.out.println(Locale.getDefault().toLanguageTag()); // en-US
final var value = 1234;
final Format format1 = NumberFormat.getCurrencyInstance();
System.out.println(format1.format(value)); // $1,234.00
final Format format2 = NumberFormat.getCurrencyInstance(Locale.JAPAN);
System.out.println(format2.format(value)); // ¥1,234
abstract StringBuffer format (Object obj, StringBuffer toAppendTo, FieldPosition pos)
final var num = 123.456;
final Format format = NumberFormat.getNumberInstance(Locale.US);
System.out.println(format.format(num)); // 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(num, 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(num, sb, pos);
System.out.println(ret); // num = 123.456
System.out.println(pos.getBeginIndex()); // 10
System.out.println(pos.getEndIndex()); // 13
}
AttributedCharacterIterator formatToCharacterIterator (Object obj)
final var num = 123;
final Format format = NumberFormat.getNumberInstance(Locale.US);
System.out.println(format.format(num)); // 123
final var it = format.formatToCharacterIterator(num);
System.out.println("-- iterator --");
for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {
System.out.println(c);
}
// Result
// ↓
//-- iterator --
//1
//2
//3
Object parseObject (String source)
System.out.println(Locale.getDefault().toLanguageTag()); // en-US
final Format format1 = NumberFormat.getCurrencyInstance();
final var ret1 = format1.parseObject("$1,234.00");
System.out.println(ret1); // 1234
final Format format2 = NumberFormat.getCurrencyInstance(Locale.JAPAN);
final var ret2 = format2.parseObject("¥1,234");
System.out.println(ret2); // 1234
abstract Object parseObject (String source, ParsePosition pos)
final Format 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
Related posts
- API Examples