BeanIO DoubleTypeHandler 模式

BeanIO DoubleTypeHandler pattern

我正在使用这个模式:

<typeHandler name="dblHandler" class="org.beanio.types.DoubleTypeHandler">
        <property name="pattern" value="##0.0000000000;-#0.0000000000"/>
</typeHandler> 

这会将数字转换为 10 位小数。但是,如果数字有 7 个小数点,它会添加 3 个尾随 0。如何更改模式以不添加尾随 0。

示例:

谢谢!

我很快写了这个来测试DecimalFormat

final DecimalFormat format = (DecimalFormat) NumberFormat.getInstance();
String pattern = "##0.0000000000;-#0.0000000000";
format.applyPattern(pattern);

System.out.println("Using pattern: " + pattern);
System.out.println("8.7829214389 = " + format.format(-8.7829214389));
System.out.println("8.7829214 = " + format.format(-8.7829214));
System.out.println("8.7829 = " + format.format(-8.7829));
System.out.println("8.123456789012 = " + format.format(-8.123456789012));
System.out.printf("%n");
System.out.println("-8.7829214389 = " + format.format(-8.7829214389));
System.out.println("-8.7829214 = " + format.format(-8.7829214));
System.out.println("-8.7829 = " + format.format(-8.7829));
System.out.println("-8.123456789012 = " + format.format(-8.123456789012));
System.out.printf("==========%n%n");

final DecimalFormat format2 = (DecimalFormat) NumberFormat.getInstance();
pattern = "##0.##########;-#0.##########";
format2.applyPattern(pattern);
System.out.println("Using pattern: " + pattern);
System.out.println("8.7829214389 = " + format2.format(8.7829214389));
System.out.println("8.7829214 = " + format2.format(8.7829214));
System.out.println("8.7829 = " + format2.format(8.7829));
System.out.println("8.123456789012 = " + format2.format(8.123456789012));
System.out.printf("%n");
System.out.println("-8.7829214389 = " + format2.format(-8.7829214389));
System.out.println("-8.7829214 = " + format2.format(-8.7829214));
System.out.println("-8.7829 = " + format2.format(-8.7829));
System.out.println("-8.123456789012 = " + format2.format(-8.123456789012));
System.out.printf("==========%n%n");

生成此输出:

Using pattern: ##0.0000000000;-#0.0000000000
8.7829214389 = -8.7829214389
8.7829214 = -8.7829214000
8.7829 = -8.7829000000
8.123456789012 = -8.1234567890

-8.7829214389 = -8.7829214389
-8.7829214 = -8.7829214000
-8.7829 = -8.7829000000
-8.123456789012 = -8.1234567890
==========

Using pattern: ##0.##########;-#0.##########
8.7829214389 = 8.7829214389
8.7829214 = 8.7829214
8.7829 = 8.7829
8.123456789012 = 8.123456789

-8.7829214389 = -8.7829214389
-8.7829214 = -8.7829214
-8.7829 = -8.7829
-8.123456789012 = -8.123456789
==========

模式中小数点后的 10 个零数字强制格式化程序始终打印小数点后的 10 位数字。如果只想打印不超过10位使用#表示小数点后的所有数字都是可选的。有关详细信息,请参阅 the Java API documentation for DecimalFormat

因此,将类型处理程序的模式更改为:

<typeHandler name="dblHandler" class="org.beanio.types.DoubleTypeHandler">
        <property name="pattern" value="##0.##########;-#0.##########"/>
</typeHandler>