Improved float value formatting.

This commit is contained in:
Michael Bayne
2011-07-19 09:32:15 -07:00
parent d3b91156b0
commit 102fb340b7
3 changed files with 33 additions and 16 deletions
+31 -4
View File
@@ -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;
}
+1 -6
View File
@@ -59,11 +59,6 @@ public class Points
* <code>+x-y</code>, <code>-x-y</code>, 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);
}
}
+1 -6
View File
@@ -74,11 +74,6 @@ public class Vectors
* <code>+x-y</code>, <code>-x-y</code>, 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);
}
}