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。

例:

    如果数字为 8.7829214389
  • ,则转换为 8.7829214389。
  • 如果数字为 8.7829214
  • ,则转换应为 8.7829214。它正在转换它到:8.7829214000
  • 如果数字为 8.7829
  • ,则转换应为 8.7829。它正在转换它至: 8.7829000000.

谢谢!

我很快写了这个来测试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 位数字,请使用 # 指示小数点后的所有数字都是可选的。有关更多详细信息,请参阅 DecimalFormat 的 Java API 文档。

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

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

相关内容

  • 没有找到相关文章

最新更新