Java Number Format Pattern getFormatter(int precision)

Here you can find the source of getFormatter(int precision)

Description

get Formatter

License

Open Source License

Declaration

public static DecimalFormat getFormatter(int precision) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

public class Main {
    static Map<Integer, String> cache = new HashMap<>();

    public static DecimalFormat getFormatter(int precision) {
        String cacheFormat = cache.get(Integer.valueOf(precision));

        if (cacheFormat != null)
            return new DecimalFormat(cacheFormat);

        StringBuilder format = new StringBuilder("##0.");

        for (int i = 0; i < precision; i++)
            format.append(0);/*from   w  ww.jav  a  2  s . co  m*/

        DecimalFormat df = new DecimalFormat(format.toString());
        cache.put(Integer.valueOf(precision), format.toString());
        return df;
    }
}

Related

  1. getFormattedNumber(String input)
  2. getFormattedQuantity(String qty, int len, String pad)
  3. getFormattedString4Digits(String number, String pattern)
  4. getFormatter()
  5. getFormatter()
  6. getFormatter(String pattern)
  7. getFormatTwoPoint(float param)
  8. getFormatWithMinimumDecimals(final int minimumDecimals, final int maximumDecimals)
  9. getGeographicPositionFormatter()