Android String Format formatVolume(Locale locale, String stringToFormat)

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

Description

Format String as a Volume 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 string formatted as a volume with group separators (eg, 131.224.000) assuming the receiver is an int string read from XML

Declaration

public static String formatVolume(Locale locale, String stringToFormat) 

Method Source Code

//package com.java2s;
/*/*ww w .  j a  va2s . c  om*/
 * (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} as a Volume
     *  
     * WARNING: Only use this method to present data to the screen
     * 
     * @param locale {@link Locale}
     * @param stringToFormat  {@link String} to format
     * @return a string formatted as a volume with group separators (eg, 131.224.000) assuming the receiver is an int string read from XML
     */
    public static String formatVolume(Locale locale, String stringToFormat) {
        if (stringToFormat == null || stringToFormat.length() == 0) {
            return null;
        }

        String result = null;

        DecimalFormat formatter = new DecimalFormat();
        formatter.setDecimalFormatSymbols(new DecimalFormatSymbols(locale));

        formatter.setGroupingUsed(true);
        formatter.setGroupingSize(3);
        formatter.setMaximumFractionDigits(0);

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

        return result;
    }
}

Related

  1. formatNumberWithDecimals(Locale locale, String stringToFormat, int exactNumberOfDecimals)
  2. formatNumberWithDecimals(Locale locale, String stringToFormat, int minimalNumberOfDecimals, int maximumNumberOfDecimals)
  3. formatNumberWithThreeDecimals(Locale locale, String stringToFormat)
  4. formatNumberWithTwoDecimals(Locale locale, String stringToFormat)
  5. formatPriceWithThreeDecimals(Locale locale, String stringToFormat)
  6. formatPriceWithTwoDecimals(Locale locale, String stringToFormat)
  7. format(String formatString, Object... args)
  8. fillSpace(String formatString, int length)
  9. fillString(String formatString, int length, char fillChar, boolean leftFillFlag)