Allow MathUtil.toString's decimals to be specified

This commit is contained in:
Charlie Groves
2011-10-05 15:04:46 -07:00
parent a9087b2c02
commit b54f684f45
2 changed files with 24 additions and 7 deletions
+12 -4
View File
@@ -196,11 +196,19 @@ public class MathUtil
}
/**
* Formats the supplied doubleing point value, truncated to the currently configured number of
* Formats the supplied double 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 (double value)
{
return toString(value, TO_STRING_DECIMAL_PLACES);
}
/**
* Formats the supplied double point value, truncated to the given number of decimal places.
* The value is also always preceded by a sign (e.g. +1.0 or -0.5).
*/
public static String toString (double value, int decimalPlaces) {
StringBuilder buf = new StringBuilder();
if (value >= 0) buf.append("+");
else {
@@ -209,15 +217,15 @@ public class MathUtil
}
int ivalue = (int)value;
buf.append(ivalue);
if (TO_STRING_DECIMAL_PLACES > 0) {
if (decimalPlaces > 0) {
buf.append(".");
for (int ii = 0; ii < TO_STRING_DECIMAL_PLACES; ii++) {
for (int ii = 0; ii < decimalPlaces; 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++) {
for (int ii = 0; ii < decimalPlaces-1; ii++) {
if (buf.charAt(buf.length()-1) == '0') {
buf.setLength(buf.length()-1);
}
+12 -3
View File
@@ -200,6 +200,15 @@ public class MathUtil
* 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 toString(value, TO_STRING_DECIMAL_PLACES);
}
/**
* Formats the supplied floating point value, truncated to the given 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, int decimalPlaces)
{
StringBuilder buf = new StringBuilder();
if (value >= 0) buf.append("+");
@@ -209,15 +218,15 @@ public class MathUtil
}
int ivalue = (int)value;
buf.append(ivalue);
if (TO_STRING_DECIMAL_PLACES > 0) {
if (decimalPlaces > 0) {
buf.append(".");
for (int ii = 0; ii < TO_STRING_DECIMAL_PLACES; ii++) {
for (int ii = 0; ii < decimalPlaces; 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++) {
for (int ii = 0; ii < decimalPlaces-1; ii++) {
if (buf.charAt(buf.length()-1) == '0') {
buf.setLength(buf.length()-1);
}