[OFBiz] SVN: r6847 - trunk/base/src/base/org/ofbiz/base/util
jonesde@svn.ofbiz.org
jonesde at svn.ofbiz.org
Sun Feb 26 00:28:48 CST 2006
Author: jonesde
Date: 2006-02-26 00:28:44 -0600 (Sun, 26 Feb 2006)
New Revision: 6847
Modified:
trunk/base/src/base/org/ofbiz/base/util/UtilNumber.java
Log:
Applied patch from Leon Torres with method to convert number to percent string with a given scale and rounding mode, based on BigDecimal; Jira OFBIZ-751
Modified: trunk/base/src/base/org/ofbiz/base/util/UtilNumber.java
===================================================================
--- trunk/base/src/base/org/ofbiz/base/util/UtilNumber.java 2006-02-25 20:54:13 UTC (rev 6846)
+++ trunk/base/src/base/org/ofbiz/base/util/UtilNumber.java 2006-02-26 06:28:44 UTC (rev 6847)
@@ -195,4 +195,27 @@
}
return result;
}
+
+ /**
+ * Method to turn a number such as "0.9853" into a nicely formatted percent, "98.53%".
+ *
+ * @param number The number object to format
+ * @param scale How many places after the decimal to include
+ * @param roundingMode The BigDecimal rounding mode to apply
+ * @return The formatted string or "" if there were errors.
+ */
+ public static String toPercentString(Number number, int scale, int roundingMode) {
+ // convert to BigDecimal
+ if (!(number instanceof BigDecimal)) {
+ number = new BigDecimal(number.doubleValue());
+ }
+
+ // cast it so we can use BigDecimal methods
+ BigDecimal bd = (BigDecimal) number;
+
+ // multiply by 100 and set the scale
+ bd = bd.multiply(new BigDecimal(100.0)).setScale(scale, roundingMode);
+
+ return (bd.toString() + "%");
+ }
}
More information about the Svn
mailing list