From 7fada6f88c9b2b2963325cffb12f055715e51eb4 Mon Sep 17 00:00:00 2001 From: SiboVG Date: Sat, 11 Jun 2022 02:38:45 +0200 Subject: [PATCH] [#1354] Export Lat & Long with 5 decimals Idk, this is not per se a very dynamic solution (as in 'you only specify these 2 exceptions somewhere hidden deep inside a method'...) Ah well, it works for now. --- .../src/net/sf/openrocket/file/CSVExport.java | 8 +- core/src/net/sf/openrocket/util/TextUtil.java | 90 ++++++++++++------- 2 files changed, 64 insertions(+), 34 deletions(-) diff --git a/core/src/net/sf/openrocket/file/CSVExport.java b/core/src/net/sf/openrocket/file/CSVExport.java index 6f821a381..7d66dfe42 100644 --- a/core/src/net/sf/openrocket/file/CSVExport.java +++ b/core/src/net/sf/openrocket/file/CSVExport.java @@ -132,7 +132,13 @@ public class CSVExport { // Store CSV line for (int i = 0; i < fields.length; i++) { double value = fieldValues.get(i).get(pos); - writer.print(TextUtil.doubleToString(units[i].toUnit(value))); + // The latitude and longitude fields need a bit more accurate formatting + if (fields[i] == FlightDataType.TYPE_LATITUDE || fields[i] == FlightDataType.TYPE_LONGITUDE) { + writer.print(TextUtil.doubleToString(units[i].toUnit(value), 5)); + } else { + writer.print(TextUtil.doubleToString(units[i].toUnit(value))); + } + if (i < fields.length - 1) { writer.print(fieldSeparator); } diff --git a/core/src/net/sf/openrocket/util/TextUtil.java b/core/src/net/sf/openrocket/util/TextUtil.java index 845923325..ffed25a90 100644 --- a/core/src/net/sf/openrocket/util/TextUtil.java +++ b/core/src/net/sf/openrocket/util/TextUtil.java @@ -40,6 +40,52 @@ public class TextUtil { } return sb.toString(); } + + /** + * Return a string of the double value with suitable precision for storage. + * The string is the shortest representation of the value including at least + * 5 digits of precision. + * + * @param d the value to present. + * @return a representation with suitable precision. + */ + public static final String doubleToString(double d, int decimalPlaces) { + // Check for special cases + if (MathUtil.equals(d, 0)) + return "0"; + + if (Double.isNaN(d)) + return "NaN"; + + if (Double.isInfinite(d)) { + if (d < 0) + return "-Inf"; + else + return "Inf"; + } + + final String sign = (d < 0) ? "-" : ""; + double abs = Math.abs(d); + + // Small and large values always in exponential notation + if (abs < 0.001 || abs >= 100000000) { + return sign + exponentialFormat(abs); + } + + // Check whether decimal or exponential notation is shorter + String exp = exponentialFormat(abs); + String dec; + if (decimalPlaces < 0) { + dec = decimalFormat(abs); + } else { + dec = decimalFormat(abs, decimalPlaces); + } + + if (dec.length() <= exp.length()) + return sign + dec; + else + return sign + exp; + } /** * Return a string of the double value with suitable precision for storage. @@ -50,39 +96,7 @@ public class TextUtil { * @return a representation with suitable precision. */ public static final String doubleToString(double d) { - - // Check for special cases - if (MathUtil.equals(d, 0)) - return "0"; - - if (Double.isNaN(d)) - return "NaN"; - - if (Double.isInfinite(d)) { - if (d < 0) - return "-Inf"; - else - return "Inf"; - } - - - final String sign = (d < 0) ? "-" : ""; - double abs = Math.abs(d); - - // Small and large values always in exponential notation - if (abs < 0.001 || abs >= 100000000) { - return sign + exponentialFormat(abs); - } - - // Check whether decimal or exponential notation is shorter - - String exp = exponentialFormat(abs); - String dec = decimalFormat(abs); - - if (dec.length() <= exp.length()) - return sign + dec; - else - return sign + exp; + return doubleToString(d, -1); } @@ -122,6 +136,16 @@ public class TextUtil { return shortDecimal(value, decimals); } + + /* + * value must be positive and not zero! + */ + private static String decimalFormat(double value, int decimals) { + if (value >= 10000) + return "" + (int) (value + 0.5); + + return shortDecimal(value, decimals); + }