Java Number Format Pattern unformatingDecimalNumber(String cadena, Locale locale, int maxIntPart, int maxFloatPart)

Here you can find the source of unformatingDecimalNumber(String cadena, Locale locale, int maxIntPart, int maxFloatPart)

Description

Translate an String to BigDecimal number.

License

Open Source License

Parameter

Parameter Description
cadena the string 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

the bigdecimal number

Declaration

public static BigDecimal unformatingDecimalNumber(String cadena, Locale locale, int maxIntPart,
        int maxFloatPart) 

Method Source Code


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

import java.math.BigDecimal;

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.*;

public class Main {
    /**// w  w w  .  jav  a 2 s  .  c  o m
     * Translate an String to BigDecimal number.
     *
     * @param cadena       the string 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 the bigdecimal number
     */
    public static BigDecimal unformatingDecimalNumber(String cadena, Locale locale, int maxIntPart,
            int maxFloatPart) {
        BigDecimal result = null;
        NumberFormat numberFormat = settingUpValuesOfFormat(locale, maxIntPart, maxFloatPart);
        if (null != cadena && !"".equals(cadena.trim())) {
            try {
                Number number = numberFormat.parse(cadena);
                result = new BigDecimal(Double.toString(number.doubleValue()));
            } catch (ParseException 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. toNumber(String numString, String numFormatPattern)
  2. toNumberFormat(String input)
  3. toString(int[] a, String separator, NumberFormat formatter)
  4. toStringFormatted(byte[] bytes)
  5. unformatAmount(String formattedNumber)