Class QuantityFormatter


public class QuantityFormatter extends Formatter<QuantityFormatContext>
QuantityFormatter formats a quantity as a String, using the settings of the QuantityFormatContext. The QuantityFormatContext is filled by using the setters in the QuantityFormat class. Note that there is no guarantee that the format settings can always be satisfied. As an example, when the required width is too small to fit the answer, the output will show the correct result, but violate the width format setting.

Copyright (c) 2026-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See for project information https://djunits.org. The DJUNITS project is distributed under a three-clause BSD-style license.

Author:
Alexander Verbraeck
  • Constructor Details

    • QuantityFormatter

      QuantityFormatter(Quantity<?> quantity, QuantityFormatContext ctx)
      Parameters:
      quantity - the quantity to format
      ctx - the quantity format context
  • Method Details

    • format

      public static String format(Quantity<?> quantity, QuantityFormat quantityFormat)
      Format a quantity according to a given QuantityFormat. Note that this method might not be thread-safe for setting the Locale. If another thread changes the Locale while formatting, outcomes could vary.
      Parameters:
      quantity - the quantity to format
      quantityFormat - the format to apply to the quantity
      Returns:
      a String with a formatted quantity, matching the given format as closely as possible
    • format

      public static String format(AbsQuantity<?,?,?> absQuantity, QuantityFormat quantityFormat)
      Format an absolute quantity according to a given QuantityFormat. Note that this method might not be thread-safe for setting the Locale. If another thread changes the Locale while formatting, outcomes could vary.
      Parameters:
      absQuantity - the absolute quantity to format
      quantityFormat - the format to apply to the absolute quantity
      Returns:
      a String with a formatted absolute quantity, matching the given format as closely as possible
    • quantity

      Quantity<?> quantity()
      Return the value as a quantity.
      Returns:
      the value as a quantity
    • format

      String format()
      Return the quantity, formatted according to the context settings.
      Specified by:
      format in class Formatter<QuantityFormatContext>
      Returns:
      the formatted quantity
    • formatUnit

      void formatUnit()
      Format the unit according to the context settings.
      Overrides:
      formatUnit in class Formatter<QuantityFormatContext>