Fixed FloatMath.toString(), added tests.

This commit is contained in:
Michael Bayne
2011-07-29 11:00:20 -07:00
parent 92f6e0932f
commit 1936e20380
2 changed files with 36 additions and 1 deletions
+5 -1
View File
@@ -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) {
@@ -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));
}
}