Android String Format formatNumberWithThreeDecimals(Locale locale, String stringToFormat)

Here you can find the source of formatNumberWithThreeDecimals(Locale locale, String stringToFormat)

Description

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

License

Apache License

Parameter

Parameter Description
locale Locale
stringToFormat String to format

Return

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

Declaration

public static String formatNumberWithThreeDecimals(Locale locale,
        String stringToFormat) 

Method Source Code

//package com.java2s;
/*/*from  ww w  .jav  a  2  s.  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 three decimals
     * 
     * WARNING: Only use this method to present data to the screen
     * 
     * @param locale {@link Locale}
     * @param stringToFormat {@link String} to format
     * @return a {@link String} formatted as a number with three decimals assuming the receiver is a float string read from XML
     */
    public static String formatNumberWithThreeDecimals(Locale locale,
            String stringToFormat) {
        return formatNumberWithDecimals(locale, stringToFormat, 3);
    }

    /***
     * 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. formattedNumber(String number)
  2. currentTime(CharSequence inFormat)
  3. formatIndent(String whiteSpace)
  4. formatNumberWithDecimals(Locale locale, String stringToFormat, int exactNumberOfDecimals)
  5. formatNumberWithDecimals(Locale locale, String stringToFormat, int minimalNumberOfDecimals, int maximumNumberOfDecimals)
  6. formatNumberWithTwoDecimals(Locale locale, String stringToFormat)
  7. formatPriceWithThreeDecimals(Locale locale, String stringToFormat)
  8. formatVolume(Locale locale, String stringToFormat)
  9. formatPriceWithTwoDecimals(Locale locale, String stringToFormat)