Java Regex Number Validate parseNumber(String in)

Here you can find the source of parseNumber(String in)

Description

parse Number

License

Open Source License

Declaration

public static long parseNumber(String in) 

Method Source Code

//package com.java2s;
/*/*from   w  w  w  .  j  a  va 2  s  .  c o m*/
 * Copyright (C) 2010-2014 Laurent CLOUET
 * Author Laurent CLOUET <laurent.clouet@nopnop.net>
 *
 * 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; version 2
 * of the License.
 *
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

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

public class Main {
    public static long parseNumber(String in) {
        in = in.trim();
        in = in.replaceAll(",", ".");
        try {
            return Long.parseLong(in);
        } catch (NumberFormatException e) {
        }
        final Matcher m = Pattern.compile("([\\d.,]+)\\s*(\\w)").matcher(in);
        m.find();
        int scale = 1;
        switch (m.group(2).charAt(0)) {
        case 'G':
        case 'g':
            scale *= 1000;
        case 'M':
        case 'm':
            scale *= 1000;
        case 'K':
        case 'k':
            scale *= 1000;
            break;
        default:
            return 0;
        }
        return Math.round(Double.parseDouble(m.group(1)) * scale);
    }
}

Related

  1. isNumeric(String text)
  2. isNumeric(String value)
  3. isNumericAndCanNull(String src)
  4. isNumericAndCanNull(String src)
  5. isNumString(String txt)
  6. parseNumber(String input)
  7. parseNumber(String input)
  8. parseNumber(String s)
  9. parseNumbers(String str)