Android String Format formatNumberWithDecimals(Locale locale, String stringToFormat, int minimalNumberOfDecimals, int maximumNumberOfDecimals)

Here you can find the source of formatNumberWithDecimals(Locale locale, String stringToFormat, int minimalNumberOfDecimals, int maximumNumberOfDecimals)

Description

Format String like a number with given decimals

License

Apache License

Parameter

Parameter Description
locale Locale
stringToFormat String to format
minimalNumberOfDecimals minimal amount of decimals. Can be any number, also negative as the used DecimalFormatter
maximumNumberOfDecimals maximum amount of decimals. Can be any number, also negative as the used DecimalFormatter

Return

a formatted as a number with given decimals assuming the receiver is a float string read from XML

Declaration

public static String formatNumberWithDecimals(Locale locale,
        String stringToFormat, int minimalNumberOfDecimals,
        int maximumNumberOfDecimals) 

Method Source Code

//package com.java2s;
/*// w w w  . jav a2s . c  o  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 {
    /***
     * 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. formatAccountNo(String accountNo)
  2. formattedNumber(String number)
  3. currentTime(CharSequence inFormat)
  4. formatIndent(String whiteSpace)
  5. formatNumberWithDecimals(Locale locale, String stringToFormat, int exactNumberOfDecimals)
  6. formatNumberWithThreeDecimals(Locale locale, String stringToFormat)
  7. formatNumberWithTwoDecimals(Locale locale, String stringToFormat)
  8. formatPriceWithThreeDecimals(Locale locale, String stringToFormat)
  9. formatVolume(Locale locale, String stringToFormat)