Java Regex Double Validate parseDouble(String s)

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

Description

Convert to a double value

License

LGPL

Declaration

public static double parseDouble(String s) 

Method Source Code

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

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    private static Pattern nf1 = Pattern.compile("[\\-\\+]?[0-9]*\\.?[0-9]+");
    private static Pattern nf2 = Pattern.compile("[\\-\\+]?\\.?[0-9]+");

    /**//ww w .j  ava  2s . co  m
     * Convert to a double value
     */
    public static double parseDouble(String s) {
        double rv = 0;
        if (s != null && s.trim().length() > 0) {
            try {
                rv = Double.parseDouble(s.trim());
            } catch (NumberFormatException nfe) {
                boolean parsed = false;
                String trimmed = s.trim();
                Matcher m1 = nf1.matcher(trimmed);
                if (m1.find()) {
                    try {
                        String pv = m1.group();
                        rv = Double.parseDouble(pv);
                        parsed = true;
                    } catch (NumberFormatException nfe1) {
                    }
                }
                if (!parsed) {
                    Matcher m2 = nf2.matcher(trimmed);
                    if (m2.find()) {
                        try {
                            String pv = m1.group();
                            rv = Double.parseDouble(pv);
                            parsed = true;
                        } catch (NumberFormatException nfe2) {
                        }
                    }
                }
            }
        }
        return rv;
    }
}

Related

  1. parseDouble(final String text)
  2. parseDouble(Object object)
  3. parseDouble(Object value, Double replace)
  4. parseDouble(String myString)
  5. parseDouble(String str)
  6. parseDoubleList(String list)