From 7bfb38491c089440df2e4cf9ddc3544e7cd8879d Mon Sep 17 00:00:00 2001 From: Alberto Fernandez Date: Fri, 26 May 2017 18:59:44 +0200 Subject: [PATCH] Zero value should be stored as 0.00 instead of .00 Fixes #30 --- .../java/com/linuxense/javadbf/DBFUtils.java | 5 ++++- .../java/com/linuxense/javadbf/UtilsTest.java | 17 ++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/linuxense/javadbf/DBFUtils.java b/src/main/java/com/linuxense/javadbf/DBFUtils.java index d15b3ad..3b9fbce 100644 --- a/src/main/java/com/linuxense/javadbf/DBFUtils.java +++ b/src/main/java/com/linuxense/javadbf/DBFUtils.java @@ -215,9 +215,12 @@ public static byte[] doubleFormating(Number num, Charset charset, int fieldLengt int sizeWholePart = fieldLength - (sizeDecimalPart > 0 ? (sizeDecimalPart + 1) : 0); StringBuilder format = new StringBuilder(fieldLength); - for (int i = 0; i < sizeWholePart; i++) { + for (int i = 0; i < sizeWholePart-1; i++) { format.append("#"); } + if (format.length() < sizeWholePart) { + format.append("0"); + } if (sizeDecimalPart > 0) { format.append("."); for (int i = 0; i < sizeDecimalPart; i++) { diff --git a/src/test/java/com/linuxense/javadbf/UtilsTest.java b/src/test/java/com/linuxense/javadbf/UtilsTest.java index fe2a3dd..f719a01 100644 --- a/src/test/java/com/linuxense/javadbf/UtilsTest.java +++ b/src/test/java/com/linuxense/javadbf/UtilsTest.java @@ -26,9 +26,12 @@ import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import org.junit.Assert; import org.junit.Test; + + public class UtilsTest { private static final Charset ISO_8859_1 = StandardCharsets.ISO_8859_1; @@ -69,7 +72,19 @@ public void testContains () { } @Test public void doubleFormating() { - // TODO + Assert.assertEquals( + " 0.00", + new String(DBFUtils.doubleFormating(new Double(0.0), Charset.defaultCharset(), 5, 2)) + ); + + Assert.assertEquals( + "10.00", + new String(DBFUtils.doubleFormating(new Double(10.0), Charset.defaultCharset(), 5, 2)) + ); + Assert.assertEquals( + " 5.05", + new String(DBFUtils.doubleFormating(new Double(5.05), Charset.defaultCharset(), 5, 2)) + ); } @Test public void testLittleEndian() {