File: NumberUtils.java

package info (click to toggle)
gpsprune 17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,984 kB
  • ctags: 5,218
  • sloc: java: 39,403; sh: 25; makefile: 17; python: 15
file content (55 lines) | stat: -rw-r--r-- 1,669 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package tim.prune.data;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

/**
 * Abstract class to offer general number manipulation functions
 */
public abstract class NumberUtils
{
	/** UK-specific number formatter object to avoid lots of instantiations */
	private static final NumberFormat UK_FORMAT = NumberFormat.getNumberInstance(Locale.UK);
	// Select the UK locale for this formatter so that decimal point is always used (not comma)
	static {
		if (UK_FORMAT instanceof DecimalFormat) ((DecimalFormat) UK_FORMAT).applyPattern("0.000");
	}

	/**
	 * Find the number of decimal places represented in the String
	 * @param inString String to check
	 * @return number of decimal places, or 0 for integer value
	 */
	public static int getDecimalPlaces(String inString)
	{
		if (inString == null || inString.equals("")) {return 0;}
		int places = 0;
		final int sLen = inString.length();
		for (int i=sLen-1; i>=0; i--) {
			char c = inString.charAt(i);
			if (c >= '0' && c <= '9') {
				// Numeric character found
				places++;
			}
			else {
				// Non-numeric character found, return places
				return places;
			}
		}
		// No non-numeric characters found, so must be integer
		return 0;
	}

	/**
	 * Format the given number in UK format (decimal point) to the given number of decimal places
	 * @param inNumber double number to format
	 * @param inDecimalPlaces number of decimal places
	 */
	public static String formatNumberUk(double inNumber, int inDecimalPlaces)
	{
		UK_FORMAT.setMaximumFractionDigits(inDecimalPlaces);
		UK_FORMAT.setMinimumFractionDigits(inDecimalPlaces);
		return UK_FORMAT.format(inNumber);
	}
}