Class MeasureFormat

  • All Implemented Interfaces:
    Serializable, Cloneable
    Direct Known Subclasses:
    TimeUnitFormat

    public class MeasureFormat
    extends UFormat
    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.

    Author:
    Alan Liu
    See Also:
    UFormat, Serialized Form
    Status:
    Stable ICU 3.0.
    • Method Detail

      • getInstance

        public static MeasureFormat getInstance​(ULocale locale,
                                                MeasureFormat.FormatWidth formatWidth)
        Create a format from the locale, formatWidth, and format.
        Parameters:
        locale - the locale.
        formatWidth - hints how long formatted strings should be.
        Returns:
        The new MeasureFormat object.
        Status:
        Stable ICU 53.
      • getInstance

        public static MeasureFormat getInstance​(Locale locale,
                                                MeasureFormat.FormatWidth formatWidth)
        Create a format from the Locale and formatWidth.
        Parameters:
        locale - the Locale.
        formatWidth - hints how long formatted strings should be.
        Returns:
        The new MeasureFormat object.
        Status:
        Stable ICU 54.
      • getInstance

        public static MeasureFormat getInstance​(ULocale locale,
                                                MeasureFormat.FormatWidth formatWidth,
                                                NumberFormat format)
        Create a format from the locale, formatWidth, and format.
        Parameters:
        locale - the locale.
        formatWidth - hints how long formatted strings should be.
        format - This is defensively copied.
        Returns:
        The new MeasureFormat object.
        Status:
        Stable ICU 53.
      • getInstance

        public static MeasureFormat getInstance​(Locale locale,
                                                MeasureFormat.FormatWidth formatWidth,
                                                NumberFormat format)
        Create a format from the Locale, formatWidth, and format.
        Parameters:
        locale - the Locale.
        formatWidth - hints how long formatted strings should be.
        format - This is defensively copied.
        Returns:
        The new MeasureFormat object.
        Status:
        Stable ICU 54.
      • format

        public