Freemarker 在处理模板时将一个小双精度数(如 0.0001)更改为零
Freemarker change a small double (like 0.0001) to zero when process a tempalte
Freemarker 版本为 2.3.28
这是freemarker模板
double value ${m.doubleValue}
StringValue value ${m.stringValue}
对于下面的代码
public class TestDouble {
public static void main(String[] args) throws IOException, TemplateException {
Configuration templateConfig;
templateConfig = new Configuration(new Version(2, 3, 0));
templateConfig.setClassLoaderForTemplateLoading(TestDouble.class.getClassLoader(), "ttt");
templateConfig.setObjectWrapper(new DefaultObjectWrapper(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS));
templateConfig.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
Map map = new HashMap();
Map m = new HashMap();
map.put("m",m);
m.put("doubleValue", 0.001);
m.put("stringValue", "0.0001");
Template template = templateConfig.getTemplate("test.ftl");
ByteArrayOutputStream output = new ByteArrayOutputStream(4096);
Writer out = new OutputStreamWriter(output);
template.process(map, out);
out.flush();
out.close();
String ret = new String(output.toByteArray());
System.out.println(ret);
}
}
会生成
double value 0.001
StringValue value 0.0001
但是当把doubleValue
改成0.0001
时,输出会是
double value 0
StringValue value 0.0001
0.0001
在输出结果中被替换为0
。是否有一些设置或格式可以利用?
添加号码格式配置:
templateConfig.setNumberFormat("0.####");
它应该可以工作,'double value' 打印了 0
,因为它已转换为 1.0E-4
。
Freemarker 版本为 2.3.28
这是freemarker模板
double value ${m.doubleValue}
StringValue value ${m.stringValue}
对于下面的代码
public class TestDouble {
public static void main(String[] args) throws IOException, TemplateException {
Configuration templateConfig;
templateConfig = new Configuration(new Version(2, 3, 0));
templateConfig.setClassLoaderForTemplateLoading(TestDouble.class.getClassLoader(), "ttt");
templateConfig.setObjectWrapper(new DefaultObjectWrapper(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS));
templateConfig.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
Map map = new HashMap();
Map m = new HashMap();
map.put("m",m);
m.put("doubleValue", 0.001);
m.put("stringValue", "0.0001");
Template template = templateConfig.getTemplate("test.ftl");
ByteArrayOutputStream output = new ByteArrayOutputStream(4096);
Writer out = new OutputStreamWriter(output);
template.process(map, out);
out.flush();
out.close();
String ret = new String(output.toByteArray());
System.out.println(ret);
}
}
会生成
double value 0.001
StringValue value 0.0001
但是当把doubleValue
改成0.0001
时,输出会是
double value 0
StringValue value 0.0001
0.0001
在输出结果中被替换为0
。是否有一些设置或格式可以利用?
添加号码格式配置:
templateConfig.setNumberFormat("0.####");
它应该可以工作,'double value' 打印了 0
,因为它已转换为 1.0E-4
。