From 1936e2038078a1e621d62930d0e03249df0c0782 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Fri, 29 Jul 2011 11:00:20 -0700 Subject: [PATCH] Fixed FloatMath.toString(), added tests. --- src/main/java/pythagoras/f/FloatMath.java | 6 +++- src/test/java/pythagoras/f/FloatMathTest.java | 31 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/test/java/pythagoras/f/FloatMathTest.java diff --git a/src/main/java/pythagoras/f/FloatMath.java b/src/main/java/pythagoras/f/FloatMath.java index 051c9d6..ba75f5d 100644 --- a/src/main/java/pythagoras/f/FloatMath.java +++ b/src/main/java/pythagoras/f/FloatMath.java @@ -388,7 +388,11 @@ public class FloatMath public static String toString (float value) { StringBuilder buf = new StringBuilder(); - buf.append(value >= 0 ? "+" : "-"); + if (value >= 0) buf.append("+"); + else { + buf.append("-"); + value = -value; + } int ivalue = (int)value; buf.append(ivalue); if (TO_STRING_DECIMAL_PLACES > 0) { diff --git a/src/test/java/pythagoras/f/FloatMathTest.java b/src/test/java/pythagoras/f/FloatMathTest.java new file mode 100644 index 0000000..c2da1f3 --- /dev/null +++ b/src/test/java/pythagoras/f/FloatMathTest.java @@ -0,0 +1,31 @@ +// +// Pythagoras - a collection of geometry classes +// http://github.com/samskivert/pythagoras + +package pythagoras.f; + +import org.junit.*; +import static org.junit.Assert.*; + +/** + * Tests parts of the {@link FloatMath} class. + */ +public class FloatMathTest +{ + @Test public void testToString () { + assertEquals("+1.0", FloatMath.toString(1f)); + assertEquals("-1.0", FloatMath.toString(-1f)); + assertEquals("+1.1", FloatMath.toString(1.1f)); + assertEquals("-1.1", FloatMath.toString(-1.1f)); + assertEquals("+3.141", FloatMath.toString(FloatMath.PI)); + assertEquals("-3.141", FloatMath.toString(-FloatMath.PI)); + + FloatMath.setToStringDecimalPlaces(5); + assertEquals("+1.0", FloatMath.toString(1f)); + assertEquals("-1.0", FloatMath.toString(-1f)); + assertEquals("+1.1", FloatMath.toString(1.1f)); + assertEquals("-1.1", FloatMath.toString(-1.1f)); + assertEquals("+3.14159", FloatMath.toString(FloatMath.PI)); + assertEquals("-3.14159", FloatMath.toString(-FloatMath.PI)); + } +}