Java Number Format Pattern toNumber(String numString, String numFormatPattern)

Here you can find the source of toNumber(String numString, String numFormatPattern)

Description

Converts a String to a Number, using the specified pattern.

License

Open Source License

Parameter

Parameter Description
numString the String to convert
numFormatPattern the pattern

Return

the corresponding Number

Declaration

public static Number toNumber(String numString, String numFormatPattern) throws ParseException 

Method Source Code

//package com.java2s;
/**//  w w  w  .ja va  2 s  . co  m
 * Copyright (C) 2002-2005 WUZEWEN. All rights reserved.
 * WUZEWEN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

import java.text.DecimalFormat;

import java.text.ParseException;

public class Main {
    private static DecimalFormat numberFormat = new DecimalFormat();

    /**
     * Converts a String to a Number, using the specified pattern.
     * (see java.text.NumberFormat for pattern description)
     *
     * @param numString the String to convert
     * @param numFormatPattern the pattern
     * @return the corresponding Number
     * @exception ParseException, if the String doesn't match the pattern
     */
    public static Number toNumber(String numString, String numFormatPattern) throws ParseException {
        Number number = null;
        if (numFormatPattern == null) {
            numFormatPattern = "######.##";
        }
        synchronized (numberFormat) {
            numberFormat.applyPattern(numFormatPattern);
            number = numberFormat.parse(numString);
        }
        return number;
    }
}

Related

  1. stringToReais(String unformatted, boolean comSimbolo)
  2. strToFormatedNumber(String str)
  3. toAccountantFormat(String str, int scale)
  4. toDecimalFormat(BigDecimal value)
  5. toFormattedNumber(Object value)
  6. toNumberFormat(String input)
  7. toString(int[] a, String separator, NumberFormat formatter)
  8. toStringFormatted(byte[] bytes)
  9. unformatAmount(String formattedNumber)