Code example for DecimalFormatSymbols

Methods: setDecimalSeparator, setGroupingSeparator

0
     * Make the data usage string smaller by replacing " MB" by "M" 
     * and rounding off some of the decimals 
     * @throws ParsingError 
     */ 
    private static String formatMbUsageForCompactness(String usage) throws ParsingError {
        DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US);
        symbols.setGroupingSeparator(' ');
        symbols.setDecimalSeparator('.');
        if (usage.matches("\\d+(\\.\\d+)? MB")) {
            // Round to 1 decimal place 
            String s = usage.replace(" MB", "");
            double d = Double.parseDouble(s);
            return new DecimalFormat("#0.#", symbols).format(d) + "M";
        } else if (usage.matches("\\d+,\\d\\d\\d(\\.\\d+)? MB")) {
            // Round of 0 decimal places 
            String s = usage.replace(",", "").replace(" MB", "");
            double d = Double.parseDouble(s);
            return new DecimalFormat("#,###", symbols).format(d) + "M";
        } else { 
            throw new ParsingError("Bad MB format"); 
        }