From c4c127d7c116241865ef7e710585ef04ddd60ce8 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 7 May 2015 09:41:50 +1200 Subject: Improve double and float format --- src/org/traccar/model/MiscFormatter.java | 13 ++++++++++++- .../traccar/model/ExtendedInfoFormatterTest.java | 20 -------------------- test/org/traccar/model/MiscFormatterTest.java | 21 +++++++++++++++++++++ 3 files changed, 33 insertions(+), 21 deletions(-) delete mode 100644 test/org/traccar/model/ExtendedInfoFormatterTest.java create mode 100644 test/org/traccar/model/MiscFormatterTest.java diff --git a/src/org/traccar/model/MiscFormatter.java b/src/org/traccar/model/MiscFormatter.java index 08a6f903a..e0e7d11e8 100644 --- a/src/org/traccar/model/MiscFormatter.java +++ b/src/org/traccar/model/MiscFormatter.java @@ -20,6 +20,7 @@ import org.traccar.helper.Log; import javax.json.Json; import javax.json.JsonObject; import javax.json.JsonObjectBuilder; +import java.text.DecimalFormat; import java.util.Map; /** @@ -29,6 +30,16 @@ public class MiscFormatter { private static final String xmlRootNode = "info"; + private static DecimalFormat decimalFormat = new DecimalFormat("#.##"); + + private static String format(Object value) { + if (value instanceof Double || value instanceof Float) { + return decimalFormat.format(value); + } else { + return value.toString(); + } + } + public static String toXmlString(Map other) { StringBuilder result = new StringBuilder(); @@ -37,7 +48,7 @@ public class MiscFormatter { for (Map.Entry entry : other.entrySet()) { result.append("<").append(entry.getKey()).append(">"); - result.append(entry.getValue()); + result.append(format(entry.getValue())); result.append(""); } diff --git a/test/org/traccar/model/ExtendedInfoFormatterTest.java b/test/org/traccar/model/ExtendedInfoFormatterTest.java deleted file mode 100644 index 9c8c86b4e..000000000 --- a/test/org/traccar/model/ExtendedInfoFormatterTest.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.traccar.model; - -import static org.junit.Assert.assertEquals; -import org.junit.Test; - -public class ExtendedInfoFormatterTest { - - @Test - public void testToString() throws Exception { - - Position position = new Position(); - position.set("a", "1"); - position.set("b", "2"); - position.set("a", "3"); - - assertEquals(position.getOther(), "32"); - - } - -} diff --git a/test/org/traccar/model/MiscFormatterTest.java b/test/org/traccar/model/MiscFormatterTest.java new file mode 100644 index 000000000..94241f95c --- /dev/null +++ b/test/org/traccar/model/MiscFormatterTest.java @@ -0,0 +1,21 @@ +package org.traccar.model; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +public class MiscFormatterTest { + + @Test + public void testToString() throws Exception { + + Position position = new Position(); + position.set("a", "1"); + position.set("b", "2"); + position.set("a", "3"); + position.set("c", 3.555); + + assertEquals(position.getOther(), "323.56"); + + } + +} -- cgit v1.2.3