X-Git-Url: http://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=src%2Ftim%2Fprune%2Fdata%2FNumberUtils.java;fp=src%2Ftim%2Fprune%2Fdata%2FNumberUtils.java;h=652e8243af2fc85d01dfb2940bc007bc0c755927;hp=0000000000000000000000000000000000000000;hb=ce6f2161b8596f7018d6a76bff79bc9e571f35fd;hpb=2d8cb72e84d5cc1089ce77baf1e34ea3ea2f8465 diff --git a/src/tim/prune/data/NumberUtils.java b/src/tim/prune/data/NumberUtils.java new file mode 100644 index 0000000..652e824 --- /dev/null +++ b/src/tim/prune/data/NumberUtils.java @@ -0,0 +1,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); + } +} \ No newline at end of file