diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-05-07 09:41:50 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-05-07 09:41:50 +1200 |
commit | c4c127d7c116241865ef7e710585ef04ddd60ce8 (patch) | |
tree | 90c368a12c5edb6eac6f84cc146f0a342fedc0ea | |
parent | 25d58792be50a96096d6a05fad94e5455139c5b8 (diff) | |
download | traccar-server-c4c127d7c116241865ef7e710585ef04ddd60ce8.tar.gz traccar-server-c4c127d7c116241865ef7e710585ef04ddd60ce8.tar.bz2 traccar-server-c4c127d7c116241865ef7e710585ef04ddd60ce8.zip |
Improve double and float format
-rw-r--r-- | src/org/traccar/model/MiscFormatter.java | 13 | ||||
-rw-r--r-- | test/org/traccar/model/MiscFormatterTest.java (renamed from test/org/traccar/model/ExtendedInfoFormatterTest.java) | 5 |
2 files changed, 15 insertions, 3 deletions
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<String, Object> other) { StringBuilder result = new StringBuilder(); @@ -37,7 +48,7 @@ public class MiscFormatter { for (Map.Entry<String, Object> entry : other.entrySet()) { result.append("<").append(entry.getKey()).append(">"); - result.append(entry.getValue()); + result.append(format(entry.getValue())); result.append("</").append(entry.getKey()).append(">"); } diff --git a/test/org/traccar/model/ExtendedInfoFormatterTest.java b/test/org/traccar/model/MiscFormatterTest.java index 9c8c86b4e..94241f95c 100644 --- a/test/org/traccar/model/ExtendedInfoFormatterTest.java +++ b/test/org/traccar/model/MiscFormatterTest.java @@ -3,7 +3,7 @@ package org.traccar.model; import static org.junit.Assert.assertEquals; import org.junit.Test; -public class ExtendedInfoFormatterTest { +public class MiscFormatterTest { @Test public void testToString() throws Exception { @@ -12,8 +12,9 @@ public class ExtendedInfoFormatterTest { position.set("a", "1"); position.set("b", "2"); position.set("a", "3"); + position.set("c", 3.555); - assertEquals(position.getOther(), "<info><a>3</a><b>2</b></info>"); + assertEquals(position.getOther(), "<info><a>3</a><b>2</b><c>3.56</c></info>"); } |