From b54f684f458b41d93d52708a8b84bc20f02f78fc Mon Sep 17 00:00:00 2001 From: Charlie Groves Date: Wed, 5 Oct 2011 15:04:46 -0700 Subject: [PATCH] Allow MathUtil.toString's decimals to be specified --- src/main/java/pythagoras/d/MathUtil.java | 16 ++++++++++++---- src/main/java/pythagoras/f/MathUtil.java | 15 ++++++++++++--- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/main/java/pythagoras/d/MathUtil.java b/src/main/java/pythagoras/d/MathUtil.java index aded971..b58052f 100644 --- a/src/main/java/pythagoras/d/MathUtil.java +++ b/src/main/java/pythagoras/d/MathUtil.java @@ -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); } diff --git a/src/main/java/pythagoras/f/MathUtil.java b/src/main/java/pythagoras/f/MathUtil.java index 16e62cc..390e710 100644 --- a/src/main/java/pythagoras/f/MathUtil.java +++ b/src/main/java/pythagoras/f/MathUtil.java @@ -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); }