Add support for %g in numeric format
Generates text that depends on changing data (like dynamic HTML).
Brought to you by:
revusky
The number_format
setting appears to require use of the java.text.DecimalFormat-style format string. This does not support %g
, which is important for handling wide dynamic-range values formatted for human readability. I have cases where the value to be formatted is equally likely to be a small number like 5.2e-6, and a large number like 5.2. In the former case, I do not want it to be formatted as 0.0000052; in the latter case I do not want it to be formatted as 5.2e0. The %g
printf()-style format specifier handles this nicely but DecimalFormat
doesn't seem to give me an equivalent.
Is there a way this could be achieved?
I have moved this over from "bugs" to "feature requests".
Currently FreeMarker doesn't support setting a number format that's not defined by
DecimalFormat
. This limitation is known to be a problem, and addressing this is quite high on the TODO list, but I don't dare to tell a date when this will be fixed. Until that, what you can do is defining aTemplateMethodModelEx
, put that into the data-model or into theConfiguration
as shared variable (or into an#import
like<#assign myFormat = 'com.example.MyFormat'?new()>
) and then format the numbers like${myFormat(x)}
.