Java Decimal Format getDecimalFormat(int precision)

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

Description

Return decimal number format.

License

Open Source License

Parameter

Parameter Description
precision a parameter

Declaration

private static NumberFormat getDecimalFormat(int precision) 

Method Source Code


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

import java.text.DecimalFormat;
import java.text.NumberFormat;

import java.util.HashMap;

import java.util.Map;

public class Main {
    private static Map<Integer, NumberFormat> decimalFormatCacheMap = new HashMap<Integer, NumberFormat>();

    /** Return decimal number format.
     *//from w  ww . java  2  s.c  o m
     *  The formats are created if it has not previously been used.
     *  Constructed formats are cached.
     *
     * @param precision
     * @return
     */
    private static NumberFormat getDecimalFormat(int precision) {
        int absPrecision = Math.abs(precision);
        NumberFormat numberFormat = decimalFormatCacheMap.get(absPrecision);
        if (numberFormat == null) {
            numberFormat = new DecimalFormat("0"); //$NON-NLS-1$
            numberFormat.setMinimumFractionDigits(absPrecision);
            numberFormat.setMaximumFractionDigits(absPrecision);
            decimalFormatCacheMap.put(absPrecision, numberFormat);
        }
        return numberFormat;
    }
}

Related

  1. getDecimalFormat()
  2. getDecimalFormat()
  3. getDecimalFormat()
  4. getDecimalFormat(final int decimalPlaces)
  5. getDecimalFormat(int decimalPlaces, Locale locale)
  6. getDecimalFormat(int precision)
  7. getDecimalFormat(int precision, String force)
  8. getDecimalFormat(String format, String sNumber)
  9. getDecimalFormat(String pattern)