Skip to content

FREEMARKER-232: Add scalePreserving number format that keeps BigDecimal scale - #134

Open
mapeders wants to merge 2 commits into
apache:2.3-gaefrom
mapeders:FREEMARKER-232
Open

mapeders wants to merge 2 commits into
apache:2.3-gaefrom
mapeders:FREEMARKER-232

Conversation

@mapeders

@mapeders mapeders commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

https://issues.apache.org/jira/browse/FREEMARKER-232

Standard formatting drops trailing zeros of BigDecimal-s (new BigDecimal("1.500") prints as 1.5).
This adds a new built-in number format name, scalePreserving, that shows exactly as many fraction
digits as the BigDecimal's scale specifies. Usage: ${x?string.scalePreserving}, or
cfg.setNumberFormat("scalePreserving"). The implementing factory,
ScalePreservingTemplateNumberFormatFactory, is public, so it can also be registered as a custom
format under an application-specific name.

Numbers that aren't BigDecimal-s have no scale, so they are formatted with a nested number format.
That's number by default, and can be specified in the format string:

  • scalePreserving|<otherFormat>, where <otherFormat> is any number format string, like
    scalePreserving|0.00##.
  • scalePreserving_<max>, or scalePreserving_<min>_<max>: like number, but with the given
    minimum and maximum fraction digits. Works with ?string too, like
    ${x?string.scalePreserving_2_6}.

?c output is intentionally unchanged, as it's meant to be canonical. Note that scalePreserving,
and strings starting with scalePreserving| or scalePreserving_, previously parsed as
(nonsensical) DecimalFormat patterns, so recognizing them is a minor behavior change — same
precedent as c in 2.3.32.

As BigDecimal division in templates uses a scale of at least 12, ${6 / 2} prints as
3.000000000000 with this format. That's only documented, along with what to do about it.

Includes unit tests, Javadoc, and Manual updates (settings reference, ?string built-in, custom
formats chapter, 2.3.36 changelog).

(I couldn't comment on the Jira issue, as I don't have an ASF Jira account yet.)

@ddekany

ddekany commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Overall it looks good. Except, I suspect we need format parameters here actually, to tell min/max digits for non-BigDecimal-s. Even when you want to show BigDecimals with their stored scale, you certainly still want to control the number of decimals shown for double/float numbers. Because it's odd to show all the digits for end-users, which easily can be 10+ decimals.

Also, the default number format uses NumberFormat.getNumberInstance(Locale), which usually rounds to 3 decimals. Kind of arbitrary... but the point is that by default FreeMarker doesn't show format with crazy number of decimals. Its defaults are more end-user (business user) oriented. So that makes the "scalePreserving" format behavior even more suprising, the first time some number is not a BigDecimal. So probably if no format parameter was specified, then we should format non-BigDecimal-s like "number" does.

@mapeders

mapeders commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor Author

@ddekany

Agreed on the default: with no parameters, non-BigDecimal-s should be formatted like number does. I'll change that.

For the parameters (which would only affect non-BigDecimal-s), I see two possible syntaxes.

A) Underscore flags, like iso_m_nz

scalePreserving_6          max 6 fraction digits
scalePreserving_2_6        min 2, max 6 fraction digits

${x?string.scalePreserving_2_6}
<#setting number_format="scalePreserving_2_6">

Consistent with the existing built-in formats and keeps ?string.name working, but the numbers aren't self-explanatory.

B) name=value, like after ;; in the extended DecimalFormat syntax

scalePreserving maxFractionDigits=6
scalePreserving minFractionDigits=2 maxFractionDigits=6

${x?string("scalePreserving minFractionDigits=2 maxFractionDigits=6")}
<#setting number_format="scalePreserving minFractionDigits=2 maxFractionDigits=6">

More readable and extensible, but a new convention for built-in format names, and ?string.name no longer works once there's a parameter.

Also: as BigDecimal division in templates uses a minimum scale of 12, ${6 / 2} prints as 3.000000000000 with this format. Should there be a way to cap the scale shown for BigDecimal-s too, or is documenting it enough?

@ddekany

ddekany commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Because this formatting really only meant to apply to BigDecimal-s, I think ScalePreservingTemplateNumberFormat should have a nested TemplateNumberFormat nonBigDecimalNumberFormat that's used in other cases.

As of the format string, it could be like "scalePreserving|<otherFormat>", where <otherFormat> can be anything that would work as a format string in itself. If there's no |, then it's effectively number.

We could still have a shorthand like "scalePreserving_<min>_<max>", "scalePreserving_<max>".

As of division, we can only document it... With this formatter, one should use an ArithmeticEngine that return dobule for divisions, unless the result has fininte decimals. I see no other way, since the intent is to show BigDecimal-s "as-is", and the division itself obviously should not round at some low number of decimals, as it's possibly used for forther calcuations, and is not even displayed directly.

… scale of BigDecimal-s, and so shows trailing 0-s
@mapeders

mapeders commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor Author

@ddekany

Thanks, I've pushed a commit that does this:

  • ScalePreservingTemplateNumberFormat has a nested TemplateNumberFormat nonBigDecimalNumberFormat, used for everything that isn't a BigDecimal.
  • "scalePreserving|<otherFormat>", where <otherFormat> is any number format string. Without | it's number.
  • Shorthand: "scalePreserving_<max>" and "scalePreserving_<min>_<max>".
  • Division is only documented (JavaDoc and Manual), pointing to the ArithmeticEngine.

…now formatted with a nested number format, which is "number" by default, and can be specified like "scalePreserving|<otherFormat>", or limited to some fraction digits like "scalePreserving_<min>_<max>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants