From eb23d68ef73a5139b2a706c1cadf2d02b3360640 Mon Sep 17 00:00:00 2001 From: Kenneth Lausdahl Date: Mon, 20 Nov 2023 00:32:15 +0100 Subject: [PATCH] added property CSV_DATA_WRITER_PRECISION to set float precision --- .../interpreter/values/csv/CsvDataWriter.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/interpreter/src/main/java/org/intocps/maestro/interpreter/values/csv/CsvDataWriter.java b/interpreter/src/main/java/org/intocps/maestro/interpreter/values/csv/CsvDataWriter.java index fad91379..45a6c272 100644 --- a/interpreter/src/main/java/org/intocps/maestro/interpreter/values/csv/CsvDataWriter.java +++ b/interpreter/src/main/java/org/intocps/maestro/interpreter/values/csv/CsvDataWriter.java @@ -17,6 +17,10 @@ public class CsvDataWriter implements IDataListener { final List filter; + static final String CSV_DATA_WRITER_PRECISION = "CSV_DATA_WRITER_PRECISION"; + + static final String floatFormatter = System.getProperty(CSV_DATA_WRITER_PRECISION) != null ? ("%." + System.getProperty( + CSV_DATA_WRITER_PRECISION) + "f") : null; private final DataFileRotater dataFileRotater; HashMap instances = new HashMap<>(); @@ -51,14 +55,18 @@ public void writeHeader(UUID uuid, List headers) { @Override public void writeDataPoint(UUID uuid, double time, List dataPoint) { List data = new Vector<>(); - data.add(Double.toString(time)); + data.add(floatFormatter == null ? Double.toString(time) : String.format(Locale.US, floatFormatter, time)); for (Integer i : instances.get(uuid).indicesOfInterest) { Value d = dataPoint.get(i).deref(); Object value = null; if (d.isNumericDecimal()) { - value = ((NumericValue) d).doubleValue(); + if (floatFormatter == null) + value = ((NumericValue) d).doubleValue(); + else { + value = String.format(Locale.US, floatFormatter, ((NumericValue) d).doubleValue()); + } } else if (d.isNumeric()) { value = ((NumericValue) d).intValue(); } else if (d instanceof BooleanValue) {