Java Parse Decimal parseDecimal(String text)

Here you can find the source of parseDecimal(String text)

Description

Returns text converted to a double-precision value or throws an exception.

License

Open Source License

Parameter

Parameter Description
text string to convert to a number
def default value to use if a ParseException is thrown

Return

the result

Declaration

public static Double parseDecimal(String text) throws ParseException 

Method Source Code


//package com.java2s;
/*//from   w  w w  . j ava 2 s .  co  m
 * This software copyright by various authors including the RPTools.net
 * development team, and licensed under the LGPL Version 3 or, at your option,
 * any later version.
 * 
 * Portions of this software were originally covered under the Apache Software
 * License, Version 1.1 or Version 2.0.
 * 
 * See the file LICENSE elsewhere in this distribution for license details.
 */

import java.text.NumberFormat;
import java.text.ParseException;

public class Main {
    private static NumberFormat nf = NumberFormat.getNumberInstance();

    /**
     * Returns <code>text</code> converted to a double-precision value, or the
     * value of <code>def</code> if the string cannot be converted. This method
     * is locale-aware.
     * 
     * @param text
     *            string to convert to a number
     * @param def
     *            default value to use if a ParseException is thrown
     * @return the result
     */
    public static Double parseDecimal(String text, Double def) {
        if (text == null)
            return def;
        try {
            return parseDecimal(text);
        } catch (ParseException e) {
            return def;
        }
    }

    /**
     * Returns <code>text</code> converted to a double-precision value or throws
     * an exception. This method is locale-aware.
     * 
     * @param text
     *            string to convert to a number
     * @param def
     *            default value to use if a ParseException is thrown
     * @return the result
     */
    public static Double parseDecimal(String text) throws ParseException {
        double def = 0.0;
        if (text == null)
            return def;
        def = nf.parse(text).doubleValue();
        //      System.out.println("Decimal:  Input string is >>"+text+"<< and parsing produces "+newValue);
        return def;
    }
}

Related

  1. parseBigDecimal(String s, NumberFormat format)
  2. parseDecimal(PushbackReader reader)
  3. parseDecimal(String d)