[#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.
This commit is contained in:
SiboVG 2022-06-11 02:38:45 +02:00
parent 72847b96eb
commit 7fada6f88c
2 changed files with 64 additions and 34 deletions

View File

@ -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);
}

View File

@ -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);
}