Package org.djunits.formatter
Class QuantityFormatter
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
-
Field Summary
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) Stringformat()Return the quantity, formatted according to the context settings.static Stringformat(AbsQuantity<?, ?, ?> absQuantity, QuantityFormat quantityFormat) Format an absolute quantity according to a givenQuantityFormat.static Stringformat(Quantity<?> quantity, QuantityFormat quantityFormat) Format a quantity according to a givenQuantityFormat.(package private) voidFormat the unit according to the context settings.(package private) Quantity<?>quantity()Return the value as a quantity.Methods inherited from class org.djunits.formatter.Formatter
checkDisplayUnit, checkSiUnits, checkUnitString, formatEngineering, formatFixedEngFallback, formatFixedFloat, formatFixedSciFallback, formatReference, formatScientific, formatValue, formatVariableLength, pad, restoreLocale, saveLocale
-
Constructor Details
-
QuantityFormatter
QuantityFormatter(Quantity<?> quantity, QuantityFormatContext ctx) - Parameters:
quantity- the quantity to formatctx- the quantity format context
-
-
Method Details
-
format
Format a quantity according to a givenQuantityFormat. 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 formatquantityFormat- the format to apply to the quantity- Returns:
- a String with a formatted quantity, matching the given format as closely as possible
-
format
Format an absolute quantity according to a givenQuantityFormat. 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 formatquantityFormat- 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:
formatin classFormatter<QuantityFormatContext>- Returns:
- the formatted quantity
-
formatUnit
void formatUnit()Format the unit according to the context settings.- Overrides:
formatUnitin classFormatter<QuantityFormatContext>
-