Example usage for java.text DecimalFormat parse

List of usage examples for java.text DecimalFormat parse

Introduction

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

Prototype

public Number parse(String source) throws ParseException 

Source Link

Document

Parses text from the beginning of the given string to produce a number.

Usage

From source file:Main.java

public static void main(String[] args) {
    System.out.println("Enter a number.");
    double numberFromConsole;
    try {/* ww  w. j ava2s  .  co m*/
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        String s = br.readLine();
        DecimalFormat df = new DecimalFormat();
        Number n = df.parse(s);
        numberFromConsole = n.doubleValue();
    } catch (IOException e) {
        numberFromConsole = 0;
    } catch (ParseException e) {
        numberFromConsole = 0;
    }
    System.out.println(numberFromConsole);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DecimalFormat format = new DecimalFormat();
    format.setParseBigDecimal(true);/*  ww w .j ava 2  s.co  m*/
    BigDecimal value = (BigDecimal) format.parse("1,234.56");
    System.out.println(value);
}

From source file:com.enitalk.controllers.paypal.Usd.java

public static void main(String[] args) throws IOException, ParseException {

    String rs = Request.Get("http://www.cbr.ru/scripts/XML_daily.asp")
            .addHeader("Content-type", "application/xml;charset=utf-8").execute().returnContent()
            .asString(Charset.forName("windows-1251"));

    Pair<AutoPilot, VTDNav> bb = getNavigator(rs.getBytes());
    String change = getChange(bb.getLeft(), bb.getRight());

    System.out.println("Rs " + change);
    DecimalFormat df = new DecimalFormat();
    DecimalFormatSymbols symbols = new DecimalFormatSymbols();
    symbols.setDecimalSeparator(',');
    df.setDecimalFormatSymbols(symbols);
    BigDecimal dd = BigDecimal.valueOf(df.parse(change).doubleValue()).setScale(2, RoundingMode.CEILING);
    System.out.println("Dd " + dd);
}

From source file:org.openconcerto.erp.core.common.element.NumerotationAutoSQLElement.java

public static void main(String[] args) {

    List<String> l = Arrays.asList("2011/05/001", "2011/05/002", "2011/05/003", "2011/05/004");
    DecimalFormat format = new DecimalFormat("'2011/05/'000");
    for (String string : l) {
        Number n;/*w  w w  .j a  v  a 2s  . co m*/
        try {
            n = format.parse(string);
            System.err.println(n);
        } catch (ParseException exn) {
            // TODO Bloc catch auto-gnr
            exn.printStackTrace();
        }
    }

}

From source file:org.apache.syncope.core.misc.DataFormat.java

public static Number parseNumber(final String source, final String conversionPattern) throws ParseException {
    DecimalFormat df = DECIMAL_FORMAT.get();
    df.applyPattern(conversionPattern);// w w w.j a v a 2 s. c  o  m
    return df.parse(source);
}

From source file:org.talend.dataprep.util.NumericHelper.java

/**
 * Checks whether <code>str</code> can be parsed by {@link BigDecimalParser} without throwing an exception.
 * @param str The string to be tested, can be <code>null</code> or empty.
 * @return <code>true</code> if string can be parsed by {@link BigDecimalParser}, <code>false</code> otherwise.
 *//*from  w w w  .ja v  a  2 s .co m*/
public static boolean isBigDecimal(String str) {
    if (StringUtils.isEmpty(str)) {
        return false;
    }
    // Check for (nnnn) values (negative values in accounting).
    String strForValidation = StringUtils.remove(str, ' ');
    if (strForValidation.lastIndexOf('(') == 0
            && strForValidation.lastIndexOf(')') == strForValidation.length() - 1) {
        strForValidation = strForValidation.substring(1, strForValidation.length() - 1); // Keep only nnnn
    }

    if (!StringUtils.containsOnly(strForValidation, ALLOWED_NUMERIC_CHARACTERS)
            && !isValid(strForValidation, new BigDecimalValidator())) {
        return false;
    }

    // Support for values that starts with ',' or '.' (like .5 or ,5).
    if (strForValidation.charAt(0) == ',' || strForValidation.charAt(0) == '.') {
        return true;
    }

    // Try custom decimal formats
    DecimalFormat[] supportedFormats = { BigDecimalParser.EU_DECIMAL_PATTERN,
            BigDecimalParser.EU_SCIENTIFIC_DECIMAL_PATTERN, BigDecimalParser.US_DECIMAL_PATTERN,
            BigDecimalParser.US_SCIENTIFIC_DECIMAL_PATTERN };
    for (DecimalFormat supportedFormat : supportedFormats) {
        try {
            if (supportedFormat.parse(strForValidation) != null) {
                return true;
            }
        } catch (ParseException e) {
            LOGGER.debug("Unable to parse '{}' using custom decimal format '{}'.", strForValidation,
                    supportedFormat.toPattern(), e);
        }
    }

    return false;
}

From source file:org.techytax.helper.AmountHelper.java

public static BigInteger parse(String amount) throws ParseException {
    if (StringUtils.isEmpty(amount)) {
        return null;
    }/*from ww  w.j a  va2s  . co m*/
    DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.GERMAN);
    otherSymbols.setDecimalSeparator(',');
    otherSymbols.setGroupingSeparator('.');
    DecimalFormat df = new DecimalFormat("###,###,###,##0", otherSymbols);
    return BigInteger.valueOf(df.parse(amount).intValue());
}

From source file:org.goko.core.common.GkUtils.java

public static BigDecimal toBigDecimal(String str) throws GkException {
    DecimalFormat df = (DecimalFormat) NumberFormat.getInstance();
    df.setParseBigDecimal(true);/*  w ww.  j  a v  a  2  s  .c  o  m*/
    try {
        return (BigDecimal) df.parse(str);
    } catch (ParseException e) {
        throw new GkTechnicalException(e);
    }
}

From source file:com.qcadoo.model.api.BigDecimalUtils.java

/**
 * Try parse string into BigDecimal.//from w  ww. ja  v a2s . c  o m
 * 
 * @param maybeStringWithDecimal
 *            String to be parsed as a BigDecimal number
 * @param locale
 *            locale to be used when parse.
 * @return either Exception that occur during parsing or parsed BigDecimal, wrapped within Optional.
 */
public static Either<Exception, Optional<BigDecimal>> tryParse(final String maybeStringWithDecimal,
        final Locale locale) {
    if (StringUtils.isBlank(maybeStringWithDecimal)) {
        return Either.right(Optional.<BigDecimal>absent());
    }
    try {
        DecimalFormat format = (DecimalFormat) DecimalFormat.getInstance(locale);
        format.setParseBigDecimal(true);
        BigDecimal parsedDecimal = new BigDecimal(format.parse(maybeStringWithDecimal).toString());
        return Either.right(Optional.fromNullable(parsedDecimal));
    } catch (Exception e) {
        return Either.left(e);
    }
}

From source file:org.jspringbot.keyword.expression.ELUtils.java

public static Double parseDouble(String str, String format) throws ParseException {
    DecimalFormat formatter = new DecimalFormat(format);

    return formatter.parse(str).doubleValue();
}