I am using this pattern:
<typeHandler name="dblHandler" class="org.beanio.types.DoubleTypeHandler">
<property name="pattern" value="##0.0000000000;-#0.0000000000"/>
</typeHandler>
This converts a number to 10 decimal points. However, if the number has 7 decimal points, it adds 3 trailing 0's. How can I change the pattern to not add trailing 0's.
Example:
Thanks!
I quickly wrote this to test 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");
Producing this output:
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
==========
The 10 zero digits after the decimal point in the pattern forces the formatter to always print 10 digits after the decimal point. If you only want to print no more than 10 digits use the #
to indicate that all the digits after the decimal point is optional. See the Java API documentation for DecimalFormat for more details.
Hence, change the pattern of your typehandler to be:
<typeHandler name="dblHandler" class="org.beanio.types.DoubleTypeHandler">
<property name="pattern" value="##0.##########;-#0.##########"/>
</typeHandler>