diff --git a/swing/src/net/sf/openrocket/gui/plot/SimulationPlot.java b/swing/src/net/sf/openrocket/gui/plot/SimulationPlot.java
index 76bb6a75c..3fbab3314 100644
--- a/swing/src/net/sf/openrocket/gui/plot/SimulationPlot.java
+++ b/swing/src/net/sf/openrocket/gui/plot/SimulationPlot.java
@@ -27,6 +27,7 @@ import net.sf.openrocket.unit.Unit;
import net.sf.openrocket.unit.UnitGroup;
import net.sf.openrocket.util.LinearInterpolator;
+import net.sf.openrocket.utils.DecimalFormatter;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.LegendItem;
@@ -263,7 +264,6 @@ public class SimulationPlot {
// Custom tooltip generator
int finalAxisno = axisno;
StandardXYToolTipGenerator tooltipGenerator = new StandardXYToolTipGenerator() {
- final DecimalFormat f = new DecimalFormat("0.00");
@Override
public String generateToolTip(XYDataset dataset, int series, int item) {
XYSeries ser = data[finalAxisno].getSeries(series);
@@ -282,14 +282,18 @@ public class SimulationPlot {
ord_end = "nd";
else if (item % 10 == 3)
ord_end = "rd";
+ double data_y = dataset.getYValue(series, item);
+ double data_x = dataset.getXValue(series, item);
+ DecimalFormat df_y = DecimalFormatter.df(data_y, 2, false);
+ DecimalFormat df_x = DecimalFormatter.df(data_x, 2, false);
return String.format("" +
"%s
" +
"Y: %s %s
" +
"X: %s %s
" +
"%d%s sample" +
"",
- name, f.format(dataset.getYValue(series, item)), unit_y,
- f.format(dataset.getXValue(series, item)), unit_x, item, ord_end);
+ name, df_y.format(data_y), unit_y,
+ df_x.format(data_x), unit_x, item, ord_end);
}
};
diff --git a/swing/src/net/sf/openrocket/utils/DecimalFormatter.java b/swing/src/net/sf/openrocket/utils/DecimalFormatter.java
new file mode 100644
index 000000000..1944cc288
--- /dev/null
+++ b/swing/src/net/sf/openrocket/utils/DecimalFormatter.java
@@ -0,0 +1,36 @@
+package net.sf.openrocket.utils;
+
+import java.text.DecimalFormat;
+
+/**
+ * This is a helper class for generating more elaborate DecimalFormats.
+ * @author SiboVanGool
+ */
+public abstract class DecimalFormatter {
+ /**
+ * Return a decimal formatter for displaying {value} with {decimals} decimals. If {value} is smaller than the second
+ * to last least significant decimal, then value will be formatted to show the 2 most significant decimals of {value}.
+ * E.g. value = 0.00345, decimals = 2 => smallest value = 0.01; value < 0.1 => format the 2 most significant decimals
+ * of value => value will be displayed as 0.0035.
+ * @param value number to be formatted
+ * @param decimals number of decimals to round off to
+ * @param display_zeros flag to check whether to display trailing zeros or not
+ * value true: display e.g. 8.00, value false, display e.g. 8
+ * @return decimal format
+ */
+ public static DecimalFormat df (double value, int decimals, boolean display_zeros) {
+ if (decimals <= 0) {
+ return new DecimalFormat("#");
+ }
+ value = Math.abs(value);
+ String pattern = "#";
+ if (display_zeros) {
+ pattern = "0";
+ }
+ if (value < Math.pow(0.1, decimals-1) && value > 0) {
+ int decimals_value = (int) Math.floor(Math.log10(1/value));
+ decimals = decimals_value + decimals;
+ }
+ return new DecimalFormat("#." + new String(new char[decimals]).replace("\0", pattern));
+ }
+}