Example usage for java.text NumberFormat parse

List of usage examples for java.text NumberFormat parse

Introduction

In this page you can find the example usage for java.text NumberFormat parse.

Prototype

public abstract Number parse(String source, ParsePosition parsePosition);

Source Link

Document

Returns a Long if possible (e.g., within the range [Long.MIN_VALUE, Long.MAX_VALUE] and with no decimals), otherwise a Double.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    NumberFormat numberFormat = NumberFormat.getNumberInstance();
    System.out.println(numberFormat.parse("123", new ParsePosition(0)));
}

From source file:Main.java

public static boolean isValidNumeric(String str) {
    NumberFormat formatter = NumberFormat.getInstance();
    ParsePosition pos = new ParsePosition(0);
    formatter.parse(str, pos);
    return str.length() == pos.getIndex();
}

From source file:utils.StringManip.java

/**
 * Checks if a string is numeric// w w  w  .j ava  2  s. c o m
 *
 * @param str String to check if it is a number
 * @return True if the string is a number
 */
public static boolean isNumeric(String str) {
    NumberFormat formatter = NumberFormat.getInstance();
    ParsePosition pos = new ParsePosition(0);
    formatter.parse(str, pos);
    boolean IsANumber = str.length() == pos.getIndex();

    Pattern p = Pattern.compile("[0-9]");
    Matcher m = p.matcher(str);
    boolean containsNumber = m.matches();

    return IsANumber || containsNumber;
}

From source file:org.apache.maven.plugin.jira.JiraHelper.java

/**
 * Try to get a JIRA pid from the issue management URL.
 *
 * @param log     Used to tell the user what happened
 * @param issueManagementUrl The URL to the issue management system
 * @param client  The client used to connect to JIRA
 * @return The JIRA id for the project, or null if it can't be found
 *//*w  w  w  .  ja va 2s . c om*/
public static String getPidFromJira(Log log, String issueManagementUrl, HttpClient client) {
    String jiraId = null;
    GetMethod gm = new GetMethod(issueManagementUrl);

    String projectPage;
    try {
        client.executeMethod(gm);
        log.debug("Successfully reached JIRA.");
        projectPage = gm.getResponseBodyAsString();
    } catch (Exception e) {
        if (log.isDebugEnabled()) {
            log.error("Unable to reach the JIRA project page:", e);
        } else {
            log.error("Unable to reach the JIRA project page. Cause is: " + e.getLocalizedMessage());
        }
        return null;
    }

    int pidIndex = projectPage.indexOf(PID);

    if (pidIndex == -1) {
        log.error("Unable to extract a JIRA pid from the page at the url " + issueManagementUrl);
    } else {
        NumberFormat nf = NumberFormat.getInstance();
        Number pidNumber = nf.parse(projectPage, new ParsePosition(pidIndex + PID.length()));
        jiraId = Integer.toString(pidNumber.intValue());
        log.debug("Found the pid " + jiraId + " at " + issueManagementUrl);
    }
    return jiraId;
}

From source file:org.totschnig.myexpenses.Utils.java

/**
 * <a href="http://www.ibm.com/developerworks/java/library/j-numberformat/">http://www.ibm.com/developerworks/java/library/j-numberformat/</a>
 * @param strFloat parsed as float with the number format defined in the locale
 * @return the float retrieved from the string or null if parse did not succeed
 *///  w  ww.j ava  2s.  co m
public static Float validateNumber(String strFloat) {
    ParsePosition pp;
    NumberFormat nfDLocal = NumberFormat.getNumberInstance();
    nfDLocal.setGroupingUsed(false);
    pp = new ParsePosition(0);
    pp.setIndex(0);
    Number n = nfDLocal.parse(strFloat, pp);
    if (strFloat.length() != pp.getIndex() || n == null) {
        return null;
    } else {
        return n.floatValue();
    }
}

From source file:com.jeeframework.util.validate.GenericTypeValidator.java

/**
 *  Checks if the value can safely be converted to a float primitive.
 *
 *@param  value   The value validation is being performed on.
 *@param  locale  The locale to use to parse the number (system default if
 *      null)//w  ww.  j a  va 2 s.  c om
 *@return the converted Float value.
 */
public static Float formatFloat(String value, Locale locale) {
    Float result = null;

    if (value != null) {
        NumberFormat formatter = null;
        if (locale != null) {
            formatter = NumberFormat.getInstance(locale);
        } else {
            formatter = NumberFormat.getInstance(Locale.getDefault());
        }
        ParsePosition pos = new ParsePosition(0);
        Number num = formatter.parse(value, pos);

        // If there was no error      and we used the whole string
        if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) {
            if (num.doubleValue() >= (Float.MAX_VALUE * -1) && num.doubleValue() <= Float.MAX_VALUE) {
                result = new Float(num.floatValue());
            }
        }
    }

    return result;
}

From source file:com.jeeframework.util.validate.GenericTypeValidator.java

/**
 *  Checks if the value can safely be converted to a double primitive.
 *
 *@param  value   The value validation is being performed on.
 *@param  locale  The locale to use to parse the number (system default if
 *      null)/*from w w  w .j  a v  a 2 s.  com*/
 *@return the converted Double value.
 */
public static Double formatDouble(String value, Locale locale) {
    Double result = null;

    if (value != null) {
        NumberFormat formatter = null;
        if (locale != null) {
            formatter = NumberFormat.getInstance(locale);
        } else {
            formatter = NumberFormat.getInstance(Locale.getDefault());
        }
        ParsePosition pos = new ParsePosition(0);
        Number num = formatter.parse(value, pos);

        // If there was no error      and we used the whole string
        if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) {
            if (num.doubleValue() >= (Double.MAX_VALUE * -1) && num.doubleValue() <= Double.MAX_VALUE) {
                result = new Double(num.doubleValue());
            }
        }
    }

    return result;
}

From source file:easyJ.common.validate.GenericTypeValidator.java

/**
 * Checks if the value can safely be converted to a float primitive.
 * //  w ww .ja v  a 2s .c o  m
 * @param value
 *                The value validation is being performed on.
 * @param locale
 *                The locale to use to parse the number (system default if
 *                null)
 * @return the converted Float value.
 */
public static Float formatFloat(String value, Locale locale) {
    Float result = null;

    if (value != null) {
        NumberFormat formatter = null;
        if (locale != null) {
            formatter = NumberFormat.getInstance(locale);
        } else {
            formatter = NumberFormat.getInstance(Locale.getDefault());
        }
        ParsePosition pos = new ParsePosition(0);
        Number num = formatter.parse(value, pos);

        // If there was no error and we used the whole string
        if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) {
            if (num.doubleValue() >= (Float.MAX_VALUE * -1) && num.doubleValue() <= Float.MAX_VALUE) {
                result = new Float(num.floatValue());
            }
        }
    }

    return result;
}

From source file:easyJ.common.validate.GenericTypeValidator.java

/**
 * Checks if the value can safely be converted to a double primitive.
 * //  w  w w  .  ja v  a  2s.co  m
 * @param value
 *                The value validation is being performed on.
 * @param locale
 *                The locale to use to parse the number (system default if
 *                null)
 * @return the converted Double value.
 */
public static Double formatDouble(String value, Locale locale) {
    Double result = null;

    if (value != null) {
        NumberFormat formatter = null;
        if (locale != null) {
            formatter = NumberFormat.getInstance(locale);
        } else {
            formatter = NumberFormat.getInstance(Locale.getDefault());
        }
        ParsePosition pos = new ParsePosition(0);
        Number num = formatter.parse(value, pos);

        // If there was no error and we used the whole string
        if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) {
            if (num.doubleValue() >= (Double.MAX_VALUE * -1) && num.doubleValue() <= Double.MAX_VALUE) {
                result = new Double(num.doubleValue());
            }
        }
    }

    return result;
}

From source file:org.pentaho.reporting.libraries.formula.typing.DefaultTypeRegistry.java

private static Number parse(final NumberFormat format, final String source) {
    final ParsePosition parsePosition = new ParsePosition(0);
    final Number result = format.parse(source, parsePosition);
    if (parsePosition.getIndex() == 0 || parsePosition.getIndex() != source.length()) {
        return null;
    }//www .  j  a  v  a  2s  .  com
    return result;
}