diff --git a/src/main/java/pythagoras/f/FloatMath.java b/src/main/java/pythagoras/f/FloatMath.java
index fd27f06..051c9d6 100644
--- a/src/main/java/pythagoras/f/FloatMath.java
+++ b/src/main/java/pythagoras/f/FloatMath.java
@@ -373,13 +373,40 @@ public class FloatMath
}
/**
- * Returns ~0 if the value is very close to zero, the string value of the float otherwise.
+ * Sets the number of decimal places to show when formatting values. By default, they are
+ * formatted to three decimal places.
+ */
+ public static void setToStringDecimalPlaces (int places) {
+ if (places < 0) throw new IllegalArgumentException("Decimal places must be >= 0.");
+ TO_STRING_DECIMAL_PLACES = places;
+ }
+
+ /**
+ * Formats the supplied floating point value, truncated to the currently configured number of
+ * decimal places. The value is also always preceded by a sign (e.g. +1.0 or -0.5).
*/
public static String toString (float value)
{
- return Math.abs(value) < ZERO ? "~0" : String.valueOf(value);
+ StringBuilder buf = new StringBuilder();
+ buf.append(value >= 0 ? "+" : "-");
+ int ivalue = (int)value;
+ buf.append(ivalue);
+ if (TO_STRING_DECIMAL_PLACES > 0) {
+ buf.append(".");
+ for (int ii = 0; ii < TO_STRING_DECIMAL_PLACES; ii++) {
+ value = (value - ivalue) * 10;
+ ivalue = (int)value;
+ buf.append(ivalue);
+ }
+ // trim trailing zeros
+ for (int ii = 0; ii < TO_STRING_DECIMAL_PLACES-1; ii++) {
+ if (buf.charAt(buf.length()-1) == '0') {
+ buf.setLength(buf.length()-1);
+ }
+ }
+ }
+ return buf.toString();
}
- /** The min value equivalent to zero. An absolute value < ZERO is rendered as ~0. */
- private static final float ZERO = 1E-7f;
+ protected static int TO_STRING_DECIMAL_PLACES = 3;
}
diff --git a/src/main/java/pythagoras/f/Points.java b/src/main/java/pythagoras/f/Points.java
index bb47c0c..6222d24 100644
--- a/src/main/java/pythagoras/f/Points.java
+++ b/src/main/java/pythagoras/f/Points.java
@@ -59,11 +59,6 @@ public class Points
* +x-y, -x-y, etc.
*/
public static String pointToString (float x, float y) {
- StringBuilder buf = new StringBuilder();
- if (x >= 0) buf.append("+");
- buf.append(FloatMath.toString(x));
- if (y >= 0) buf.append("+");
- buf.append(FloatMath.toString(y));
- return buf.toString();
+ return FloatMath.toString(x) + FloatMath.toString(y);
}
}
diff --git a/src/main/java/pythagoras/f/Vectors.java b/src/main/java/pythagoras/f/Vectors.java
index 403d8b1..9575995 100644
--- a/src/main/java/pythagoras/f/Vectors.java
+++ b/src/main/java/pythagoras/f/Vectors.java
@@ -74,11 +74,6 @@ public class Vectors
* +x-y, -x-y, etc.
*/
public static String vectorToString (float x, float y) {
- StringBuilder buf = new StringBuilder();
- if (x >= 0) buf.append("+");
- buf.append(x);
- if (y >= 0) buf.append("+");
- buf.append(y);
- return buf.toString();
+ return FloatMath.toString(x) + FloatMath.toString(y);
}
}