Android Double Format formatPriceWithTwoDecimals(Locale locale, double doubleToFormat)

Here you can find the source of formatPriceWithTwoDecimals(Locale locale, double doubleToFormat)

Description

Format String like a price with two decimals WARNING: Only use this method to present data to the screen

License

Apache License

Parameter

Parameter Description
locale Locale
doubleToFormat double to format

Return

a formatted as a price with two decimals assuming the receiver is a float string read from XML

Declaration

public static String formatPriceWithTwoDecimals(Locale locale,
        double doubleToFormat) 

Method Source Code

//package com.java2s;
/*/* ww  w.  j  a va  2 s .co  m*/
 * (C) Copyright Itude Mobile B.V., The Netherlands
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

import java.util.Locale;

public class Main {

    public static String formatPriceWithTwoDecimals(Locale locale,
            String stringToFormat) {
        if (stringToFormat == null || stringToFormat.length() == 0) {
            return null;
        }

        int numberStart = stringToFormat.indexOf(" ");
        if (numberStart != -1
                || (stringToFormat.indexOf("$") > -1 || stringToFormat
                        .indexOf("?") > -1)) {
            numberStart = Math.max(stringToFormat.indexOf("$"),
                    stringToFormat.indexOf("?"));
        }

        if (numberStart > -1) {
            String prefix = stringToFormat.substring(0, numberStart + 1);
            stringToFormat = stringToFormat.substring(numberStart + 1,
                    stringToFormat.length());
            return prefix
                    + formatNumberWithDecimals(locale, stringToFormat, 2);
        }
        return formatNumberWithDecimals(locale, stringToFormat, 2);
    }

    /**
     * Format {@link String} like a price with two decimals
     *  
     * WARNING: Only use this method to present data to the screen
     * 
     * @param locale {@link Locale}
     * @param doubleToFormat double to format
     * @return a {@link String} formatted as a price with two decimals assuming the receiver is a float string read from XML
     */
    public static String formatPriceWithTwoDecimals(Locale locale,
            double doubleToFormat) {
        DecimalFormat formatter = new DecimalFormat();
        setupFormatter(formatter, locale, 2);

        return formatter.format(doubleToFormat);
    }

    /***
     * Format {@link String} like a number with given decimals
     * 
     * @param stringToFormat {@link String} to format
     * @param exactNumberOfDecimals can be any number, also negative as the used DecimalFormatter accepts it and makes it 0
     * @return a {@link String} formatted as a number with given decimals assuming the receiver is a float string read from XML
     */
    public static String formatNumberWithDecimals(Locale locale,
            String stringToFormat, int exactNumberOfDecimals) {
        return formatNumberWithDecimals(locale, stringToFormat,
                exactNumberOfDecimals, exactNumberOfDecimals);
    }

    /**
     * 
     * Format {@link String} like a number with given decimals
     * 
     * @param locale {@link Locale}
     * @param stringToFormat {@link String} to format
     * @param minimalNumberOfDecimals minimal amount of decimals. Can be any number, also negative as the used DecimalFormatter 
     * @param maximumNumberOfDecimals maximum amount of decimals. Can be any number, also negative as the used DecimalFormatter 
     * @return a {@link String} formatted as a number with given decimals assuming the receiver is a float string read from XML
     */
    public static String formatNumberWithDecimals(Locale locale,
            String stringToFormat, int minimalNumberOfDecimals,
            int maximumNumberOfDecimals) {
        if (stringToFormat == null || stringToFormat.length() == 0) {
            return null;
        }

        String result = null;

        DecimalFormat formatter = new DecimalFormat();
        setupFormatter(formatter, locale, minimalNumberOfDecimals,
                maximumNumberOfDecimals);

        result = formatter.format(Double.parseDouble(stringToFormat));

        return result;
    }

    /**
     * Set the formatter.
     * 
     * @param formatter {@link DecimalFormat}
     * @param locale {@link Locale}
     * @param numDec number of decimals
     */
    private static void setupFormatter(DecimalFormat formatter,
            Locale locale, int numDec) {
        setupFormatter(formatter, locale, numDec, numDec);
    }

    /**
     * Set the formatter.
     * 
     * @param formatter {@link DecimalFormat}
     * @param locale {@link Locale}
     * @param minimalDecimalNumbers minimal number of decimals
     * @param maximumDecimalNumbers maximum number of decimals
     */
    private static void setupFormatter(DecimalFormat formatter,
            Locale locale, int minimalDecimalNumbers,
            int maximumDecimalNumbers) {
        formatter.setDecimalFormatSymbols(new DecimalFormatSymbols(locale));
        formatter.setMinimumIntegerDigits(1);
        formatter.setMinimumFractionDigits(minimalDecimalNumbers);
        formatter.setMaximumFractionDigits(maximumDecimalNumbers);
        formatter.setGroupingUsed(true);
        formatter.setGroupingSize(3);
    }
}

Related

  1. formatToTwoDecimalPlaces(String decimal)
  2. decimalFormat(int i, Double num)
  3. priceFormat(double price, String pattern)
  4. formatCurrency(int cents)
  5. formatBalance(Double balance, String curr)