在用springboot整合freemarker的标签输出数字的时候,超过3位数字就会自动加上逗号,需要去除逗号,试了settings.setProperty("number_format", "#")发现并不能解决,方法如下:
@Configuration
public class FreeMarkerConfig {
@Bean
public FreeMarkerConfigurer freeMarkerConfigurer() throws IOException, TemplateException {
FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
factory.setTemplateLoaderPath("");
factory.setDefaultEncoding("UTF-8");
factory.setPreferFileSystemAccess(false);
freemarker.template.Configuration configuration = factory.createConfiguration();
configuration.setClassicCompatible(true);
freeMarkerConfigurer.setConfiguration(configuration);
//数字格式去除逗号
Properties settings = new Properties();
settings.setProperty("number_format", "#");
freeMarkerConfigurer.setFreemarkerSettings(settings);
return freeMarkerConfigurer;
}
}
经过多次配置测试后发现使用configuration.setNumberFormat("#")可以解决问题,方法如下:
@Bean
public FreeMarkerConfigurer freeMarkerConfigurer() throws IOException, TemplateException {
FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
factory.setTemplateLoaderPath("classpath:/templates/"+theme+"/");
factory.setDefaultEncoding("UTF-8");
factory.setPreferFileSystemAccess(false);
freemarker.template.Configuration configuration = factory.createConfiguration();
configuration.setClassicCompatible(true);
//数字格式去除逗号
configuration.setNumberFormat("#");
freeMarkerConfigurer.setConfiguration(configuration);
return freeMarkerConfigurer;
}
在SpringBoot应用中整合Freemarker模板引擎时,遇到一个数字格式化的问题,即当数字超过三位时,Freemarker会自动添加逗号分隔符。博主通过尝试设置`number_format`属性为`#`未能解决问题。最终,通过调用`configuration.setNumberFormat(#)`方法成功地去除了数字中的逗号。这一解决方案对于需要精确控制数字格式输出的场景尤其关键。
964

被折叠的 条评论
为什么被折叠?



