Class Formatter<C extends FormatContext>

java.lang.Object
org.djunits.formatter.Formatter<C>
Type Parameters:
C - the specific FormatContext that is used
Direct Known Subclasses:
MatrixFormatter, QuantityFormatter, TableFormatter, VectorFormatter

public abstract class Formatter<C extends FormatContext> extends Object
Formatter of quantities, vectors, matrices and tables according to the format options that are stored in the FormatContext or one of its extensions.

Copyright (c) 2025-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, Peter Knoppers
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    (package private) final C
    the format context.
    (package private) Unit<?,?>
    the unit to express the value in.
    (package private) String
    the formatted unit.
    (package private) boolean
    using SI value or valueInUnit for the calculated unit.
    (package private) final Value<?,?>
    the value (quantity, vector, matrix) with a display unit.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Formatter(Value<?,?> value, C ctx)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) boolean
    Apply the display unit if present.
    (package private) boolean
    Check if SI formatting needs to be applied.
    (package private) boolean
    Apply the unit string if present.
    (package private) abstract String
    Format the value(s) and unit and return the String representation.
    (package private) String
    formatEngineering(double val)
    Format a value using engineering notation with a fixed length and a given number of decimals.
    (package private) String
    Format a value using fixed length, but when it does not fit, fall back to engineering notation.
    (package private) String
    formatFixedFloat(double val)
    Format a value with a fixed floating point length and a given number of decimals.
    (package private) String
    Format a value using fixed length, but when it does not fit, fall back to scientific notation.
    (package private) static String
    formatReference(FormatContext ctx, Reference<?,?,?> reference)
    Format the reference of an absolute value according to the context settings.
    (package private) String
    formatScientific(double val)
    Format a value using scientific notation with a fixed length and a given number of decimals.
    (package private) void
    Format the unit according to the context settings.
    (package private) String
    formatValue(double val)
    Format a value according to the context settings.
    (package private) String
    Format a value with variable length.
    (package private) static String
    pad(String s, int width)
    Pad a string with spaces.
    (package private) static void
    restoreLocale(Locale oldLocale)
    Restore the locale to the old locale.
    (package private) static Locale
    saveLocale(Locale newLocale)
    Save the current locale, and change the locale.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • ctx

      final C extends FormatContext ctx
      the format context.
    • value

      final Value<?,?> value
      the value (quantity, vector, matrix) with a display unit.
    • unit

      Unit<?,?> unit
      the unit to express the value in.
    • unitStr

      String unitStr
      the formatted unit.
    • useSi

      boolean useSi
      using SI value or valueInUnit for the calculated unit.
  • Constructor Details

    • Formatter

      Formatter(Value<?,?> value, C ctx)
      Parameters:
      value - the value to format
      ctx - the format context
  • Method Details

    • format

      abstract String format()
      Format the value(s) and unit and return the String representation.
      Returns:
      the formatted value and unit
    • checkSiUnits

      boolean checkSiUnits()
      Check if SI formatting needs to be applied.
      Returns:
      whether SI formatting was applied
    • checkUnitString

      boolean checkUnitString()
      Apply the unit string if present.
      Returns:
      whether unit string formatting was applied
    • checkDisplayUnit

      boolean checkDisplayUnit()
      Apply the display unit if present.
      Returns:
      whether display unit formatting was applied
    • formatUnit

      void formatUnit()
      Format the unit according to the context settings.
    • formatReference

      static String formatReference(FormatContext ctx, Reference<?,?,?> reference)
      Format the reference of an absolute value according to the context settings.
      Parameters:
      ctx - the format context with the settings for formatting a reference
      reference - the reference to format
      Returns:
      the formatted reference, or an empty string when it is not displayed
    • formatValue

      String formatValue(double val)
      Format a value according to the context settings.
      Parameters:
      val - the value to format
      Returns:
      the formatted value according to the context settings
    • formatVariableLength

      String formatVariableLength(double val)
      Format a value with variable length.
      Parameters:
      val - the value to format
      Returns:
      a formatted value with variable length
    • formatFixedFloat

      String formatFixedFloat(double val)
      Format a value with a fixed floating point length and a given number of decimals.
      Parameters:
      val - the value to format
      Returns:
      a formatted value with a fixed floating point length and a given number of decimals
    • formatScientific

      String formatScientific(double val)
      Format a value using scientific notation with a fixed length and a given number of decimals.
      Parameters:
      val - the value to format
      Returns:
      a formatted value using scientific notation with a fixed length and a given number of decimals
    • formatEngineering

      String formatEngineering(double val)
      Format a value using engineering notation with a fixed length and a given number of decimals.
      Parameters:
      val - the value to format
      Returns:
      a formatted value using engineering notation with a fixed length and a given number of decimals
    • formatFixedSciFallback

      String formatFixedSciFallback(double val)
      Format a value using fixed length, but when it does not fit, fall back to scientific notation.
      Parameters:
      val - the value to format
      Returns:
      a formatted value using fixed length, but when it does not fit, fall back to scientific notation.
    • formatFixedEngFallback

      String formatFixedEngFallback(double val)
      Format a value using fixed length, but when it does not fit, fall back to engineering notation.
      Parameters:
      val - the value to format
      Returns:
      a formatted value using fixed length, but when it does not fit, fall back to engineering notation.
    • pad

      static String pad(String s, int width)
      Pad a string with spaces.
      Parameters:
      s - the string to pad
      width - the width
      Returns:
      a padded string
    • saveLocale

      static Locale saveLocale(Locale newLocale)
      Save the current locale, and change the locale.
      Parameters:
      newLocale - the new locale (can be null if the locale does not change)
      Returns:
      the old locale, or null when the locale was not changed
    • restoreLocale

      static void restoreLocale(Locale oldLocale)
      Restore the locale to the old locale.
      Parameters:
      oldLocale - the old locale (can be null if the locale was not changed earlier)