Java Regex Double Validate parseDouble(final String text)

Here you can find the source of parseDouble(final String text)

Description

If the next token is a double and return its value.

License

Open Source License

Declaration

public static double parseDouble(final String text) throws NumberFormatException 

Method Source Code

//package com.java2s;

import java.util.regex.Pattern;

public class Main {
    private static final Pattern DOUBLE_INFINITY = Pattern.compile("-?inf(inity)?", Pattern.CASE_INSENSITIVE);

    /**//from  www  . j  av a 2 s .  c  o  m
     * If the next token is a double and return its value.
     * Otherwise, throw a {@link NumberFormatException}.
     */
    public static double parseDouble(final String text) throws NumberFormatException {
        // We need to parse infinity and nan separately because
        // Double.parseDouble() does not accept "inf", "infinity", or "nan".
        if (DOUBLE_INFINITY.matcher(text).matches()) {
            final boolean negative = text.startsWith("-");
            return negative ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
        }
        if (text.equalsIgnoreCase("nan")) {
            return Double.NaN;
        }

        final double result = Double.parseDouble(text);
        return result;
    }
}

Related

  1. parseDouble(Object object)
  2. parseDouble(Object value, Double replace)
  3. parseDouble(String myString)
  4. parseDouble(String s)