Java : ChoiceFormat with Examples

ChoiceFormat (Java SE 18 & JDK 18) API Examples.
You will find code examples on most ChoiceFormat methods.


Summary

A ChoiceFormat allows you to attach a format to a range of numbers.

Class diagram

final double[] limits = {-1.0, 0.0, ChoiceFormat.nextDouble(0.0)};
System.out.println(Arrays.toString(limits)); // [-1.0, 0.0, 4.9E-324]

final String[] formats = {"Negative!", "Zero!", "Positive!"};
final var format = new ChoiceFormat(limits, formats);

//              x < limits[0] ...             x < -1.0     ... Negative!
// limits[0] <= x < limits[1] ...     -1.0 <= x < 0.0      ... Negative!
// limits[1] <= x < limits[2] ...      0.0 <= x < 4.9E-314 ... Zero!
// limits[2] <= x             ... 4.9E-314 <= x            ... Positive!

System.out.println(format.format(-0.123)); // Negative!
System.out.println(format.format(-456.0)); // Negative!
System.out.println(format.format(0.0)); // Zero!
System.out.println(format.format(789.0)); // Positive!

Fields declared in NumberFormat

FRACTION_FIELD, INTEGER_FIELD

Please see the link below.

Constructors

ChoiceFormat (double[] limits, String[] formats)

Constructs with the limits and the corresponding formats.

final double[] limits = {-1.0, 0.0, ChoiceFormat.nextDouble(0.0)};
System.out.println(Arrays.toString(limits)); // [-1.0, 0.0, 4.9E-324]

final String[] formats = {"Negative!", "Zero!", "Positive!"};
final var format = new ChoiceFormat(limits, formats);

//              x < limits[0] ...             x < -1.0     ... Negative!
// limits[0] <= x < limits[1] ...     -1.0 <= x < 0.0      ... Negative!
// limits[1] <= x < limits[2] ...      0.0 <= x < 4.9E-314 ... Zero!
// limits[2] <= x             ... 4.9E-314 <= x            ... Positive!

System.out.println(format.format(-0.123)); // Negative!
System.out.println(format.format(-456.0)); // Negative!
System.out.println(format.format(0.0)); // Zero!
System.out.println(format.format(789.0)); // Positive!

ChoiceFormat (String newPattern)

Constructs with limits and corresponding formats based on the pattern.

final var pattern = "0#no files| 1#one file| 1<many files";
final var format = new ChoiceFormat(pattern);

System.out.println(format.format(0.0)); // no files
System.out.println(format.format(0.5)); // no files
System.out.println(format.format(1.0)); // one file
System.out.println(format.format(1.5)); // many files
System.out.println(format.format(2.0)); // many files

Methods

void applyPattern (String newPattern)

Sets the pattern.

final var format = new ChoiceFormat("1#one| 2#two");
System.out.println(format.toPattern()); // 1.0#one|2.0#two

System.out.println(format.format(1.0)); // one
System.out.println(format.format(2.0)); // two

format.applyPattern("1#first|2#second");
System.out.println(format.toPattern()); // 1.0#first|2.0#second

System.out.println(format.format(1.0)); // first
System.out.println(format.format(2.0)); // second

Object clone ()

Overrides Cloneable

final var format = new ChoiceFormat("1#one| 2#two");
System.out.println(format.toPattern()); // 1.0#one|2.0#two

if (format.clone() instanceof ChoiceFormat cloned) {
    System.out.println(format.toPattern()); // 1.0#one|2.0#two
    System.out.println(format.equals(cloned)); // true
}

boolean equals (Object obj)

Equality comparison between two

final var format1 = new ChoiceFormat("1#one| 2#two");
final var format2 = new ChoiceFormat("1#one| 2#two");
final var format3 = new ChoiceFormat("1#first|2#second");

System.out.println(format1.equals(format2)); // true
System.out.println(format1.equals(format3)); // false
System.out.println(format2.equals(format3)); // false

StringBuffer format (double number, StringBuffer toAppendTo, FieldPosition status)

Returns pattern with formatted double.

final var number = 2.0;

final var format = new ChoiceFormat("1#one| 2#two");
System.out.println(format.format(number)); // two

final var sb = new StringBuffer("num = ");
System.out.println(sb); // num =

final var status = new FieldPosition(NumberFormat.INTEGER_FIELD);
final var ret = format.format(number, sb, status);
System.out.println(ret); // num = two

// ignore no useful status is returned.
System.out.println(status.getBeginIndex()); // 0
System.out.println(status.getEndIndex()); // 0

StringBuffer format (long number, StringBuffer toAppendTo, FieldPosition status)

Specialization of format.

This method is equivalent to format((double)number, toAppendTo, status).

Object[] getFormats ()

Get the formats passed in the constructor.

final var format = new ChoiceFormat(
        new double[]{1.0, 2.0},
        new String[]{"one", "two"}
);

System.out.println(Arrays.toString(format.getLimits())); // [1.0, 2.0]
System.out.println(Arrays.toString(format.getFormats())); // [one, two]

System.out.println(format.format(1.0)); // one
System.out.println(format.format(2.0)); // two

format.setChoices(
        new double[]{1.0, 2.0, 3.0},
        new String[]{"first", "second", "third"}
);

System.out.println(Arrays.toString(format.getLimits())); // [1.0, 2.0, 3.0]
System.out.println(Arrays.toString(format.getFormats())); // [first, second, third]

System.out.println(format.format(1.0)); // first
System.out.println(format.format(2.0)); // second
System.out.println(format.format(3.0)); // third

double[] getLimits ()

Get the limits passed in the constructor.

Please see getFormats().

int hashCode ()

Generates a hash code for the message format object.

final var format = new ChoiceFormat(
        new double[]{1.0, 2.0},
        new String[]{"one", "two"}
);
System.out.println(format.hashCode()); // 115278
final var format = new ChoiceFormat("1#first|2#second");
System.out.println(format.hashCode()); // -906279818

static final double nextDouble (double d)

Finds the least double greater than d.

final var ret1 = ChoiceFormat.nextDouble(-1.0);
System.out.println(ret1); // -0.9999999999999999

final var ret2 = ChoiceFormat.nextDouble(0.0);
System.out.println(ret2); // 4.9E-324

final var ret3 = ChoiceFormat.nextDouble(1.0);
System.out.println(ret3); // 1.0000000000000002

static double nextDouble (double d, boolean positive)

Finds the least double greater than d (if positive is true), or the greatest double less than d (if positive is false).

final var ret1 = ChoiceFormat.nextDouble(-1.0, true);
System.out.println(ret1); // -0.9999999999999999

final var ret2 = ChoiceFormat.nextDouble(-1.0, false);
System.out.println(ret2); // -1.0000000000000002
final var ret1 = ChoiceFormat.nextDouble(0.0, true);
System.out.println(ret1); // 4.9E-324

final var ret2 = ChoiceFormat.nextDouble(0.0, false);
System.out.println(ret2); // -4.9E-324
final var ret1 = ChoiceFormat.nextDouble(1.0, true);
System.out.println(ret1); // 1.0000000000000002

final var ret2 = ChoiceFormat.nextDouble(1.0, false);
System.out.println(ret2); // 0.9999999999999999

Number parse (String text, ParsePosition status)

Parses a Number from the input text.

final var format = new ChoiceFormat(
        new double[]{1.0, 2.0},
        new String[]{"one", "two"}
);

final var source = "one two";
final var pos = new ParsePosition(0);

final var ret1 = format.parse(source, pos);
System.out.println(ret1); // 1.0
System.out.println(pos.getIndex()); // 3

pos.setIndex(pos.getIndex() + 1);

final var ret2 = format.parse(source, pos);
System.out.println(ret2); // 2.0
System.out.println(pos.getIndex()); // 7

final var ret3 = format.parse(source, pos);
System.out.println(ret3); // NaN

static final double previousDouble (double d)

Finds the greatest double less than d.

final var ret1 = ChoiceFormat.previousDouble(-1.0);
System.out.println(ret1); // -1.0000000000000002

final var ret2 = ChoiceFormat.previousDouble(0.0);
System.out.println(ret2); // -4.9E-324

final var ret3 = ChoiceFormat.previousDouble(1.0);
System.out.println(ret3); // 0.9999999999999999

void setChoices (double[] limits, String[] formats)

Set the choices to be used in formatting.

Please see getFormats().

String toPattern ()

Gets the pattern.

Please see applyPattern(String newPattern).

Methods declared in NumberFormat

format, format, format, getAvailableLocales, getCompactNumberInstance, getCompactNumberInstance, getCurrency, getCurrencyInstance, getCurrencyInstance, getInstance, getInstance, getIntegerInstance, getIntegerInstance, getMaximumFractionDigits, getMaximumIntegerDigits, getMinimumFractionDigits, getMinimumIntegerDigits, getNumberInstance, getNumberInstance, getPercentInstance, getPercentInstance, getRoundingMode, isGroupingUsed, isParseIntegerOnly, parse, parseObject, setCurrency, setGroupingUsed, setMaximumFractionDigits, setMaximumIntegerDigits, setMinimumFractionDigits, setMinimumIntegerDigits, setParseIntegerOnly, setRoundingMode

Please see the link below.

Methods declared in Format

format, formatToCharacterIterator, parseObject

Please see the link below.


Related posts

To top of page