Java Locale Format formatingDecimalNumber(Object obj, Locale locale, int maxIntPart, int maxFloatPart)

Here you can find the source of formatingDecimalNumber(Object obj, Locale locale, int maxIntPart, int maxFloatPart)

Description

formatting the object (obj) to a bigDecimal taking into account the locale

License

Open Source License

Parameter

Parameter Description
obj the object that contains the number
locale the locale
maxIntPart this the max of digits on the integer part
maxFloatPart this the max of digits on the decimal part

Return

Object that contains the bigdecimal formatted for the views

Declaration

public static Object formatingDecimalNumber(Object obj, Locale locale, int maxIntPart, int maxFloatPart) 

Method Source Code

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

import java.text.NumberFormat;

import java.util.*;

public class Main {
    /**//  w w  w  .j  a va  2 s. c  om
     * formatting the object (obj) to a bigDecimal taking into account the locale
     *
     * @param obj          the object that contains the number
     * @param locale       the locale
     * @param maxIntPart   this the max of digits on the integer part
     * @param maxFloatPart this the max of digits on the decimal part
     * @return Object that contains the bigdecimal formatted for the views
     */
    public static Object formatingDecimalNumber(Object obj, Locale locale, int maxIntPart, int maxFloatPart) {
        Object result = obj;

        NumberFormat numberFormat = settingUpValuesOfFormat(locale, maxIntPart, maxFloatPart);
        try {
            Double number = new Double(obj.toString());
            result = numberFormat.format(number.doubleValue());
        } catch (NumberFormatException e) {
        }
        return result;
    }

    /**
     * Setting up the values for the instance an numberformat
     *
     * @param locale       the locale
     * @param maxIntPart   this the max of digits on the integer part
     * @param maxFloatPart this the max of digits on the decimal part
     * @return numberformat
     */
    private static NumberFormat settingUpValuesOfFormat(Locale locale, int maxIntPart, int maxFloatPart) {
        NumberFormat numberFormat = NumberFormat.getInstance(locale);
        numberFormat.setMaximumFractionDigits(maxFloatPart);
        numberFormat.setMaximumIntegerDigits(maxIntPart);
        return numberFormat;
    }
}

Related

  1. formatFloat(float f, int numDecPlaces, boolean forceFractions)
  2. formatFloat(Float f, int style)
  3. formatFloat(float val)
  4. formatFraction2(final Number value)
  5. formatInCurrentDefaultLocale(double d)
  6. formatInt(final int number)
  7. formatInteger(int input)
  8. formatInteger(Long num)
  9. formatIntWithCommas(int num)