Java Number Add readDoubleAsString(String doubleAsString)

Here you can find the source of readDoubleAsString(String doubleAsString)

Description

Method for reading a double value as a string which uses either "," or "."

License

Apache License

Parameter

Parameter Description
doubleAsString the double value as a string

Exception

Parameter Description
NumberFormatException thrown if the double cannot be read as adouble

Return

the double value

Declaration

public static double readDoubleAsString(String doubleAsString)
        throws NumberFormatException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.math.BigDecimal;

public class Main {
    /**//from   ww w.ja v a2 s  .co m
     * Method for reading a double value as a string which uses either "," or
     * "." as the decimal symbol.
     *
     * @param doubleAsString the double value as a string
     * @return the double value
     * @throws NumberFormatException thrown if the double cannot be read as a
     * double
     */
    public static double readDoubleAsString(String doubleAsString)
            throws NumberFormatException {

        BigDecimal temp;
        try {
            temp = new BigDecimal(doubleAsString);
        } catch (NumberFormatException e) {
            doubleAsString = doubleAsString.replaceAll("\\.", "");
            doubleAsString = doubleAsString.replaceAll(",", "\\.");
            try {
                temp = new BigDecimal(doubleAsString);
            } catch (NumberFormatException ex) {
                throw new NumberFormatException(doubleAsString
                        + " cannot be read as a floating value!");
            }
        }

        return temp.doubleValue();
    }
}

Related

  1. add4Money(Double value1, Double value2)
  2. addAmounts(final double num1, final double num2)
  3. addDoubles(Double value, Double addValue)
  4. addDoubles(String str1, String str2, int rounding)
  5. DoubleToInt(double d, double multiply, double add)