1 package tim.prune.data;
3 import java.text.DecimalFormat;
4 import java.text.NumberFormat;
5 import java.util.Locale;
8 * Abstract class to offer general number manipulation functions
10 public abstract class NumberUtils
12 /** Number formatter object to avoid lots of instantiations */
13 private static final NumberFormat NUM_FORMATTER = NumberFormat.getNumberInstance(Locale.UK);
14 // Select the UK locale for this formatter so that decimal point is always used (not comma)
16 if (NUM_FORMATTER instanceof DecimalFormat) ((DecimalFormat) NUM_FORMATTER).applyPattern("0.000");
20 * Find the number of decimal places represented in the String
21 * @param inString String to check
22 * @return number of decimal places, or 0 for integer value
24 public static int getDecimalPlaces(String inString)
26 if (inString == null || inString.equals("")) {return 0;}
28 final int sLen = inString.length();
29 for (int i=sLen-1; i>=0; i--) {
30 char c = inString.charAt(i);
31 if (c >= '0' && c <= '9') {
32 // Numeric character found
36 // Non-numeric character found, return places
40 // No non-numeric characters found, so must be integer
45 * Format the given number to the given number of decimal places
46 * @param inNumber double number to format
47 * @param inDecimalPlaces number of decimal places
49 public static String formatNumber(double inNumber, int inDecimalPlaces)
51 NUM_FORMATTER.setMaximumFractionDigits(inDecimalPlaces);
52 NUM_FORMATTER.setMinimumFractionDigits(inDecimalPlaces);
53 return NUM_FORMATTER.format(inNumber);