01: /**
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */package com.tc.util.stringification;
04:
05: import com.tc.util.Assert;
06:
07: /**
08: * Contains methods for pretty-printing various things.
09: */
10: public class PrettyPrintUtils {
11:
12: public static String pluralize(String base, int quantity) {
13: if (quantity == 1)
14: return base;
15: else if (base.trim().toLowerCase().endsWith("s"))
16: return base + "es";
17: else
18: return base + "s";
19: }
20:
21: public static String quantity(String ofWhat, int howMany) {
22: return "" + howMany + " " + pluralize(ofWhat, howMany);
23: }
24:
25: public static String percentage(double value,
26: int howManyDecimalDigits) {
27: Assert.eval(howManyDecimalDigits >= 0);
28:
29: value *= 100.0;
30:
31: int integral = howManyDecimalDigits > 0 ? (int) value
32: : (int) Math.round(value);
33: int fraction = (int) Math
34: .round(Math.abs(value - integral) * 100.0);
35:
36: String integralPart = Integer.toString(integral);
37: String fractionPart = Integer.toString(fraction);
38: while (fractionPart.length() < howManyDecimalDigits)
39: fractionPart = "0" + fractionPart;
40:
41: if (howManyDecimalDigits == 0)
42: return integralPart + "%";
43: else
44: return integralPart + "." + fractionPart + "%";
45: }
46:
47: }
|