gpt4 book ai didi

java - BeanIO DoubleTypeHandler 模式

转载 作者:行者123 更新时间:2023-12-02 02:20:47 35 4
gpt4 key购买 nike

我正在使用这种模式:

<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 位,请使用# 表示小数点后的所有数字都是可选的。请参阅 the Java API documentation for DecimalFormat 了解更多详细信息。

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

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

关于java - BeanIO DoubleTypeHandler 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48532661/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com