freemarker.core

## Class TemplateNumberFormat

- java.lang.Object
- - freemarker.core.TemplateValueFormat
  - - freemarker.core.TemplateNumberFormat

    public abstract class TemplateNumberFormat
    extends TemplateValueFormat

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

Constructors
Constructor and Description
`TemplateNumberFormat()`

### Method Summary

All Methods Instance Methods Abstract Methods Concrete Methods
`Object` `format(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` `isLocaleBound()` Tells if this formatter should be re-created if the locale changes.
`Object` `parse(String s)` This method is reserved for future purposes; currently it always throws `ParsingNotSupportedException`.

### Methods inherited from class freemarker.core.TemplateValueFormat

`getDescription`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Constructor Detail

#### TemplateNumberFormat

    public TemplateNumberFormat()

### Method Detail

#### formatToPlainText

    public abstract String formatToPlainText(TemplateNumberModel numberModel)
                                      throws TemplateValueFormatException,
                                             TemplateModelException

Parameters:
  `numberModel` - The number to format; not `null`. Most implementations will just work with the return value of `TemplateDateModel.getAsDate()`, but some may format differently depending on the properties of a custom `TemplateDateModel` 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 the `dateModel` object when calling its methods.

#### format

    public Object format(TemplateNumberModel numberModel)
                  throws TemplateValueFormatException,
                         TemplateModelException

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. If the markup result would be just the result of `formatToPlainText(TemplateNumberModel)` escaped, it must return the `String` that `formatToPlainText(TemplateNumberModel)` does.

The implementation in `TemplateNumberFormat` simply calls `formatToPlainText(TemplateNumberModel)`.

Returns:
  A `String` or a `TemplateMarkupOutputModel`; not `null`.
Throws:
  `TemplateValueFormatException`
  `TemplateModelException`

#### isLocaleBound

    public abstract boolean isLocaleBound()

Tells if this formatter should be re-created if the locale changes.

#### parse

    public final Object parse(String s)
                       throws TemplateValueFormatException

This method is reserved for future purposes; currently it always throws `ParsingNotSupportedException`. We don't yet support number parsing with `TemplateNumberFormat`-s, because currently FTL parses strings to number with the `ArithmeticEngine` (`TemplateNumberFormat` were only introduced in 2.3.24). If it will be support, it will be similar to `TemplateDateFormat.parse(String, int)`.

Throws:
  `TemplateValueFormatException`