如何在Freemarker GAE v2.3.22中实现自定义数字格式?
Hey there! Let's figure out how to create custom number formats for FreeMarker GAE 2.3.22. Since this is an older version, the official docs might not highlight this as prominently as newer releases, but we have two solid approaches to make this work.
Approach 1: Custom TemplateNumberFormatFactory (Java-side)
This method gives you full control and is ideal if you need reusable, locale-aware formats. Here's how to implement it:
Step 1: Create a Custom Format Factory
Implement the TemplateNumberFormatFactory interface to define your formatting logic:
import freemarker.core.TemplateNumberFormat; import freemarker.core.TemplateNumberFormatFactory; import freemarker.template.Configuration; import freemarker.template.TemplateModelException; import java.text.DecimalFormat; import java.util.Locale; import java.util.Map; public class CustomCurrencyFormatFactory implements TemplateNumberFormatFactory { @Override public TemplateNumberFormat get(String params, Locale locale, Map<String, Object> paramsMap) throws TemplateModelException { // Define your custom number pattern here DecimalFormat customFormat = new DecimalFormat("$#,##0.00;($#,##0.00)"); return new TemplateNumberFormat() { @Override public String formatToPlainTextNumber(Number number) { return customFormat.format(number); } @Override public boolean isLocaleBound() { return false; // Set to true if your format depends on the locale } @Override public String getDescription() { return "Custom currency format with parentheses for negative values"; } }; } }
Step 2: Register the Factory with FreeMarker Configuration
Add your custom factory to the FreeMarker configuration so templates can reference it:
Configuration freemarkerConfig = new Configuration(Configuration.VERSION_2_3_22); // Register the format with a unique name (e.g., "custom_currency") freemarkerConfig.setCustomNumberFormat("custom_currency", new CustomCurrencyFormatFactory());
Step 3: Use the Custom Format in Templates
Call your custom format using the ?string built-in:
<#assign positiveAmount = 1234.56> <#assign negativeAmount = -789.01> Positive amount formatted: ${positiveAmount?string("custom_currency")} Negative amount formatted: ${negativeAmount?string("custom_currency")}
Approach 2: Template-Side Macro (No Java Code)
If you don't want to write Java classes, you can create a reusable macro directly in your FreeMarker templates for simpler formatting needs:
<#macro customNumberFormat num> <#if num >= 0> $${num?string(",##0.00")} <#else> ($${(-num)?string(",##0.00")}) </#if> </#macro> <!-- Use the macro in your template --> <@customNumberFormat num=1234.56/> <@customNumberFormat num=-789.01/>
Key Notes for GAE Environment
- Ensure any Java classes you use (like
DecimalFormat) are allowed in GAE's sandbox—standard Java text formatting classes are fully supported. - For locale-specific formats, adjust the
isLocaleBoundflag in theTemplateNumberFormatimplementation and use thelocaleparameter passed to thegetmethod.
Hope this helps you get your custom number formatting up and running smoothly! If you hit any issues with the examples, feel free to tweak them to match your exact formatting requirements.
内容的提问来源于stack exchange,提问作者Shantanu Lawande

