Added in API level 24

MeasureFormat


open class MeasureFormat : UFormat
kotlin.Any
   ↳ java.text.Format
   ↳ android.icu.text.UFormat
   ↳ android.icu.text.MeasureFormat

A formatter for Measure objects.

IMPORTANT: New users are strongly encouraged to see if NumberFormatter fits their use case. Although not deprecated, this class, MeasureFormat, is provided for backwards compatibility only, and has much more limited capabilities.

To format a Measure object, first create a formatter object using a MeasureFormat factory method. Then use that object's format or formatMeasures methods. Here is sample code:

MeasureFormat fmtFr = MeasureFormat.getInstance(ULocale.FRENCH, FormatWidth.SHORT);
  Measure measure = new Measure(23, MeasureUnit.CELSIUS);
 
  // Output: 23 °C
  System.out.println(fmtFr.format(measure));
 
  Measure measureF = new Measure(70, MeasureUnit.FAHRENHEIT);
 
  // Output: 70 °F
  System.out.println(fmtFr.format(measureF));
 
  MeasureFormat fmtFrFull = MeasureFormat.getInstance(ULocale.FRENCH, FormatWidth.WIDE);
  // Output: 70 pieds et 5,3 pouces
  System.out.println(fmtFrFull.formatMeasures(new Measure(70, MeasureUnit.FOOT),
          new Measure(5.3, MeasureUnit.INCH)));
 
  // Output: 1 pied et 1 pouce
  System.out.println(
          fmtFrFull.formatMeasures(new Measure(1, MeasureUnit.FOOT), new Measure(1, MeasureUnit.INCH)));
 
  MeasureFormat fmtFrNarrow = MeasureFormat.getInstance(ULocale.FRENCH, FormatWidth.NARROW);
  // Output: 1′ 1″
  System.out.println(fmtFrNarrow.formatMeasures(new Measure(1, MeasureUnit.FOOT),
          new Measure(1, MeasureUnit.INCH)));
 
  MeasureFormat fmtEn = MeasureFormat.getInstance(ULocale.ENGLISH, FormatWidth.WIDE);
 
  // Output: 1 inch, 2 feet
  fmtEn.formatMeasures(new Measure(1, MeasureUnit.INCH), new Measure(2, MeasureUnit.FOOT));
  

This class does not do conversions from one unit to another. It simply formats whatever units it is given

This class is immutable and thread-safe so long as its deprecated subclass, TimeUnitFormat, is never used. TimeUnitFormat is not thread-safe, and is mutable. Although this class has existing subclasses, this class does not support new sub-classes.

Summary

Nested classes

Formatting width enum.

Public methods
Boolean
equals(other: Any?)

Two MeasureFormats, a and b, are equal if and only if they have the same formatWidth, locale, and equal number formats.

open StringBuffer!
format(obj: Any!, toAppendTo: StringBuffer!, fpos: FieldPosition!)

Able to format Collection<? extends Measure>, Measure[], and Measure by delegating to formatMeasures.

open StringBuilder!
formatMeasurePerUnit(measure: Measure!, perUnit: MeasureUnit!, appendTo: StringBuilder!, pos: FieldPosition!)

Formats a single measure per unit.

String!
formatMeasures(vararg measures: Measure!)

Format a sequence of measures.

open StringBuilder!
formatMeasures(appendTo: StringBuilder!, fpos: FieldPosition!, vararg measures: Measure!)

Formats a sequence of measures.

open static MeasureFormat!

Return a formatter for CurrencyAmount objects in the given locale.

open static MeasureFormat!

Return a formatter for CurrencyAmount objects in the given java.util.Locale.

open static MeasureFormat!

Return a formatter for CurrencyAmount objects in the default FORMAT locale.

open static MeasureFormat!
getInstance(locale: ULocale!, formatWidth: MeasureFormat.FormatWidth!)

Create a format from the locale, formatWidth, and format.

open static MeasureFormat!
getInstance(locale: Locale!, formatWidth: MeasureFormat.FormatWidth!)

Create a format from the java.util.Locale and formatWidth.

open static MeasureFormat!
getInstance(locale: ULocale!, formatWidth: MeasureFormat.FormatWidth!, format: NumberFormat!)

Create a format from the locale, formatWidth, and format.

open static MeasureFormat!
getInstance(locale: Locale!, formatWidth: MeasureFormat.FormatWidth!, format: NumberFormat!)

Create a format from the java.util.Locale, formatWidth, and format.

ULocale!

Get the locale of this instance.

open NumberFormat!

Get a copy of the number format.

open String!

Gets the display name of the specified MeasureUnit corresponding to the current locale and format width.

open MeasureFormat.FormatWidth!

Get the format width this instance is using.

Int

Returns a hash code value for the object.

Public methods

equals

Added in API level 24
fun equals(other: Any?): Boolean

Two MeasureFormats, a and b, are equal if and only if they have the same formatWidth, locale, and equal number formats.

Parameters
obj the reference object with which to compare.
Return
Boolean true if this object is the same as the obj argument; false otherwise.

format

Added in API level 24
open fun format(
    obj: Any!,
    toAppendTo: StringBuffer!,
    fpos: FieldPosition!
): StringBuffer!

Able to format Collection<? extends Measure>, Measure[], and Measure by delegating to formatMeasures. If the pos argument identifies a NumberFormat field, then its indices are set to the beginning and end of the first such field encountered. MeasureFormat itself does not supply any fields. Calling a formatMeasures method is preferred over calling this method as they give better performance.

Parameters
obj Any!: must be a Collection<? extends Measure>, Measure[], or Measure object.
toAppendTo StringBuffer!: Formatted string appended here.
pos A FieldPosition identifying a field in the formatted text
fpos FieldPosition!: Identifies a field in the formatted text.
Return
StringBuffer! the string buffer passed in as toAppendTo, with formatted text appended
Exceptions
java.lang.NullPointerException if toAppendTo or pos is null
java.lang.IllegalArgumentException if the Format cannot format the given object

formatMeasurePerUnit

Added in API level 26
open fun formatMeasurePerUnit(
    measure: Measure!,
    perUnit: MeasureUnit!,
    appendTo: StringBuilder!,
    pos: FieldPosition!
): StringBuilder!

Formats a single measure per unit. An example of such a formatted string is "3.5 meters per second."

Parameters
measure Measure!: the measure object. In above example, 3.5 meters.
perUnit MeasureUnit!: the per unit. In above example, it is MeasureUnit.SECOND
appendTo StringBuilder!: formatted string appended here.
pos FieldPosition!: The field position.
Return
StringBuilder! appendTo.

formatMeasures

Added in API level 24
fun formatMeasures(vararg measures: Measure!): String!

Format a sequence of measures. Uses the ListFormatter unit lists. So, for example, one could format “3 feet, 2 inches”. Zero values are formatted (eg, “3 feet, 0 inches”). It is the caller’s responsibility to have the appropriate values in appropriate order, and using the appropriate Number values. Typically the units should be in descending order, with all but the last Measure having integer values (eg, not “3.2 feet, 2 inches”).

Parameters
measures Measure!: a sequence of one or more measures.
Return
String! the formatted string.

formatMeasures

Added in API level 24
open fun formatMeasures(
    appendTo: StringBuilder!,
    fpos: FieldPosition!,
    vararg measures: Measure!
): StringBuilder!

Formats a sequence of measures. If the fieldPosition argument identifies a NumberFormat field, then its indices are set to the beginning and end of the first such field encountered. MeasureFormat itself does not supply any fields.

Parameters
appendTo StringBuilder!: the formatted string appended here.
fpos FieldPosition!: Identifies a field in the formatted text.
measures Measure!: the measures to format.
Return
StringBuilder! appendTo.

getCurrencyFormat

Added in API level 24
open static fun getCurrencyFormat(locale: ULocale!): MeasureFormat!

Return a formatter for CurrencyAmount objects in the given locale.

Parameters
locale ULocale!: desired locale
Return
MeasureFormat! a formatter object

getCurrencyFormat

Added in API level 24
open static fun getCurrencyFormat(locale: Locale!): MeasureFormat!

Return a formatter for CurrencyAmount objects in the given java.util.Locale.

Parameters
locale Locale!: desired java.util.Locale
Return
MeasureFormat! a formatter object

getCurrencyFormat

Added in API level 24
open static fun getCurrencyFormat(): MeasureFormat!

Return a formatter for CurrencyAmount objects in the default FORMAT locale.

Return
MeasureFormat! a formatter object

getInstance

Added in API level 24
open static fun getInstance(
    locale: ULocale!,
    formatWidth: MeasureFormat.FormatWidth!
): MeasureFormat!

Create a format from the locale, formatWidth, and format.

Parameters
locale ULocale!: the locale.
formatWidth MeasureFormat.FormatWidth!: hints how long formatted strings should be.
Return
MeasureFormat! The new MeasureFormat object.

getInstance

Added in API level 24
open static fun getInstance(
    locale: Locale!,
    formatWidth: MeasureFormat.FormatWidth!
): MeasureFormat!

Create a format from the java.util.Locale and formatWidth.

Parameters
locale Locale!: the java.util.Locale.
formatWidth MeasureFormat.FormatWidth!: hints how long formatted strings should be.
Return
MeasureFormat! The new MeasureFormat object.

getInstance

Added in API level 24
open static fun getInstance(
    locale: ULocale!,
    formatWidth: MeasureFormat.FormatWidth!,
    format: NumberFormat!
): MeasureFormat!

Create a format from the locale, formatWidth, and format.

Parameters
locale ULocale!: the locale.
formatWidth MeasureFormat.FormatWidth!: hints how long formatted strings should be.
format NumberFormat!: This is defensively copied.
Return
MeasureFormat! The new MeasureFormat object.

getInstance

Added in API level 24
open static fun getInstance(
    locale: Locale!,
    formatWidth: MeasureFormat.FormatWidth!,
    format: NumberFormat!
): MeasureFormat!

Create a format from the java.util.Locale, formatWidth, and format.

Parameters
locale Locale!: the java.util.Locale.
formatWidth MeasureFormat.FormatWidth!: hints how long formatted strings should be.
format NumberFormat!: This is defensively copied.
Return
MeasureFormat! The new MeasureFormat object.

getLocale

Added in API level 24
fun getLocale(): ULocale!

Get the locale of this instance.

getNumberFormat

Added in API level 24
open fun getNumberFormat(): NumberFormat!

Get a copy of the number format.

getUnitDisplayName

Added in API level 28
open fun getUnitDisplayName(unit: MeasureUnit!): String!

Gets the display name of the specified MeasureUnit corresponding to the current locale and format width.

Parameters
unit MeasureUnit!: The unit for which to get a display name.
Return
String! The display name in the locale and width specified in android.icu.text.MeasureFormat#getInstance, or null if there is no display name available for the specified unit.

getWidth

Added in API level 24
open fun getWidth(): MeasureFormat.FormatWidth!

Get the format width this instance is using.

hashCode

Added in API level 24
fun hashCode(): Int

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Return
Int a hash code value for this object.