Java Parse Double parseDouble(String value)

Here you can find the source of parseDouble(String value)

Description

Parse a string containing a double with locale specific formatting e.g.

License

Open Source License

Declaration

public static double parseDouble(String value) 

Method Source Code

//package com.java2s;
/*// ww w.  j a v a 2  s.com
 Grammatical Evolution in Java
 Release: GEVA-v2.0.zip
 Copyright (C) 2008 Michael O'Neill, Erik Hemberg, Anthony Brabazon, Conor Gilligan 
 Contributors Patrick Middleburgh, Eliott Bartley, Jonathan Hugosson, Jeff Wrigh

 Separate licences for asm, bsf, antlr, groovy, jscheme, commons-logging, jsci is included in the lib folder. 
 Separate licence for rieps is included in src/com folder.

 This licence refers to GEVA-v2.0.

 This software is distributed under the terms of the GNU General Public License.


 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 />.
 */

import java.text.DecimalFormatSymbols;

public class Main {
    /**
     * Parse a string containing a double with locale specific formatting
     * e.g. 1.000,00 is formatted to 1000.00 and then parsed to double
     */
    public static double parseDouble(String value) {
        return Double.parseDouble(parsePrepare(value));
    }

    /**
     * Ruin all locale specific formatting for a parsable equivalent
     * @param value
     * @return
     */
    private static String parsePrepare(String value) {
        DecimalFormatSymbols symbols = new DecimalFormatSymbols();
        char group = symbols.getGroupingSeparator();
        char decimal = symbols.getDecimalSeparator();
        return value.trim().replace(String.valueOf(group), "")
                .replace(decimal, '.');
    }

    /**
     * Trim an array of strings so that all padding white-space is removed,
     *  e.g. {" a ", " ", "b"} -> {"a", "", "b"}
     * @param a An array of strings to trim
     * @return The array of strings with no white-space padding
     */
    public static String[] trim(String[] a) {
        for (int i = 0; i < a.length; i++)
            a[i] = a[i].trim();
        return a;
    }
}

Related

  1. parseDouble(String inStr)
  2. parseDouble(String s, Locale locale)
  3. parseDouble(String str)
  4. parseDouble(String str)
  5. parseDouble(String stringValue)
  6. ParseDoubleEx(String s, double value_if_fault)
  7. parseDoubleInCurrentDefaultLocale(String text)
  8. parseInternationalDouble(String number)
  9. parseToCientificNotation(double value)