Package freemarker.core
Class TemplateNumberFormat
java.lang.Object
freemarker.core.TemplateValueFormat
freemarker.core.TemplateNumberFormat
Represents a number format; used in templates for formatting and parsing with that format. This is similar to Java's
NumberFormat
, but made to fit the requirements of FreeMarker. Also, it makes easier to define formats that
can't be represented with Java's existing NumberFormat
implementations.
Implementations need not be thread-safe if the TemplateNumberFormatFactory
doesn't recycle them among
different Environment
-s. As far as FreeMarker's concerned, instances are bound to a single
Environment
, and Environment
-s are thread-local objects.
- Since:
- 2.3.24
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionformat(TemplateNumberModel numberModel)
Formats the model to markup instead of to plain text if the result markup will be more than just plain text escaped, otherwise falls back to formatting to plain text.abstract String
formatToPlainText(TemplateNumberModel numberModel)
abstract boolean
Tells if this formatter should be re-created if the locale changes.This method is reserved for future purposes; currently it always throwsParsingNotSupportedException
.Methods inherited from class freemarker.core.TemplateValueFormat
getDescription
-
Constructor Details
-
TemplateNumberFormat
public TemplateNumberFormat()
-
-
Method Details
-
formatToPlainText
public abstract String formatToPlainText(TemplateNumberModel numberModel) throws TemplateValueFormatException, TemplateModelException- Parameters:
numberModel
- The number to format; notnull
. Most implementations will just work with the return value ofTemplateDateModel.getAsDate()
, but some may format differently depending on the properties of a customTemplateDateModel
implementation.- Returns:
- The number as text, with no escaping (like no HTML escaping); can't be
null
. - Throws:
TemplateValueFormatException
- If any problem occurs while parsing/getting the format. Notable subclass:UnformattableValueException
.TemplateModelException
- Exception thrown by thedateModel
object when calling its methods.
-
format
public Object format(TemplateNumberModel numberModel) throws TemplateValueFormatException, TemplateModelExceptionFormats the model to markup instead of to plain text if the result markup will be more than just plain text escaped, otherwise falls back to formatting to plain text. If the markup result would be just the result offormatToPlainText(TemplateNumberModel)
escaped, it must return theString
thatformatToPlainText(TemplateNumberModel)
does.The implementation in
TemplateNumberFormat
simply callsformatToPlainText(TemplateNumberModel)
.- Returns:
- A
String
or aTemplateMarkupOutputModel
; notnull
. - Throws:
TemplateValueFormatException
TemplateModelException
-
isLocaleBound
public abstract boolean isLocaleBound()Tells if this formatter should be re-created if the locale changes. -
parse
This method is reserved for future purposes; currently it always throwsParsingNotSupportedException
. We don't yet support number parsing withTemplateNumberFormat
-s, because currently FTL parses strings to number with theArithmeticEngine
(TemplateNumberFormat
were only introduced in 2.3.24). If it will be support, it will be similar toTemplateDateFormat.parse(String, int)
.- Throws:
TemplateValueFormatException
-