Merge pull request #6 from groves/master
Allow MathUtil.toString's decimals to be specified
This commit is contained in:
@@ -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).
|
* decimal places. The value is also always preceded by a sign (e.g. +1.0 or -0.5).
|
||||||
*/
|
*/
|
||||||
public static String toString (double value)
|
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();
|
StringBuilder buf = new StringBuilder();
|
||||||
if (value >= 0) buf.append("+");
|
if (value >= 0) buf.append("+");
|
||||||
else {
|
else {
|
||||||
@@ -209,15 +217,15 @@ public class MathUtil
|
|||||||
}
|
}
|
||||||
int ivalue = (int)value;
|
int ivalue = (int)value;
|
||||||
buf.append(ivalue);
|
buf.append(ivalue);
|
||||||
if (TO_STRING_DECIMAL_PLACES > 0) {
|
if (decimalPlaces > 0) {
|
||||||
buf.append(".");
|
buf.append(".");
|
||||||
for (int ii = 0; ii < TO_STRING_DECIMAL_PLACES; ii++) {
|
for (int ii = 0; ii < decimalPlaces; ii++) {
|
||||||
value = (value - ivalue) * 10;
|
value = (value - ivalue) * 10;
|
||||||
ivalue = (int)value;
|
ivalue = (int)value;
|
||||||
buf.append(ivalue);
|
buf.append(ivalue);
|
||||||
}
|
}
|
||||||
// trim trailing zeros
|
// 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') {
|
if (buf.charAt(buf.length()-1) == '0') {
|
||||||
buf.setLength(buf.length()-1);
|
buf.setLength(buf.length()-1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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).
|
* decimal places. The value is also always preceded by a sign (e.g. +1.0 or -0.5).
|
||||||
*/
|
*/
|
||||||
public static String toString (float value)
|
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();
|
StringBuilder buf = new StringBuilder();
|
||||||
if (value >= 0) buf.append("+");
|
if (value >= 0) buf.append("+");
|
||||||
@@ -209,15 +218,15 @@ public class MathUtil
|
|||||||
}
|
}
|
||||||
int ivalue = (int)value;
|
int ivalue = (int)value;
|
||||||
buf.append(ivalue);
|
buf.append(ivalue);
|
||||||
if (TO_STRING_DECIMAL_PLACES > 0) {
|
if (decimalPlaces > 0) {
|
||||||
buf.append(".");
|
buf.append(".");
|
||||||
for (int ii = 0; ii < TO_STRING_DECIMAL_PLACES; ii++) {
|
for (int ii = 0; ii < decimalPlaces; ii++) {
|
||||||
value = (value - ivalue) * 10;
|
value = (value - ivalue) * 10;
|
||||||
ivalue = (int)value;
|
ivalue = (int)value;
|
||||||
buf.append(ivalue);
|
buf.append(ivalue);
|
||||||
}
|
}
|
||||||
// trim trailing zeros
|
// 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') {
|
if (buf.charAt(buf.length()-1) == '0') {
|
||||||
buf.setLength(buf.length()-1);
|
buf.setLength(buf.length()-1);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user