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 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) throws Exception {
    NumberFormat numberFormat = NumberFormat.getNumberInstance();
    System.out.println(numberFormat.parse("123"));
}

From source file:MainClass.java

public static void main(String[] av) {

    String input = "4096.251";
    NumberFormat defForm = NumberFormat.getInstance();

    try {/*from  w w w  .  ja  v a 2 s. c om*/
        Number d = defForm.parse(input);
        System.out.println(input + " parses as " + d + " and formats as " + defForm.format(d));
    } catch (ParseException pe) {
        System.err.println(input + "not parseable!");
    }
}

From source file:NumFormatParse.java

/** The main (and only) method in this class. */
public static void main(String[] av) {

    //+//from www  .ja v  a 2 s .  c o m
    NumberFormat defForm = NumberFormat.getInstance();

    try {
        Number d = defForm.parse(input);
        System.out.println(input + " parses as " + d + " and formats as " + defForm.format(d));
    } catch (ParseException pe) {
        System.err.println(input + "not parseable!");
    }
    //-
}

From source file:ConstantLocaleUsage.java

public static void main(String[] argv) {

    NumberFormat numberFormat = NumberFormat.getInstance();
    numberFormat.setParseIntegerOnly(false);
    double usersNumber;

    if (argv.length == 1)
        try {/*from  www .j  ava 2s  . c  om*/
            usersNumber = numberFormat.parse(argv[0]).doubleValue();
        } catch (ParseException e) {
            usersNumber = 197912.29;
        }
    else
        usersNumber = 1976.0826;

    numberFormat = NumberFormat.getNumberInstance(Locale.US);
    System.out.println("User's number (US): " + numberFormat.format(usersNumber));
    numberFormat = NumberFormat.getNumberInstance(Locale.GERMANY);
    System.out.println("User's number (GERMANY): " + numberFormat.format(usersNumber));
    numberFormat = NumberFormat.getNumberInstance();
    System.out.println("User's number (DEFAULT LOCALE): " + numberFormat.format(usersNumber));
}

From source file:Main.java

/**
 * Convert a string number into a double value
 *
 * @param text the text to be converted to number
 * @return the double value//www. j a  v  a2  s  .  c  om
 */
private static double stringToDouble(String text) {

    text = text.replaceAll(",", ".");
    NumberFormat nf = NumberFormat.getInstance(Locale.US);
    try {
        return nf.parse(text).doubleValue();
    } catch (ParseException e) {
        Log.e(TAG, e.getMessage(), e);
        return 0.0;
    }
}

From source file:Main.java

public static Double string2double(String str) {
    Double returnVal = Double.valueOf(0);
    try {//  w ww  . ja  v  a  2 s  .co  m
        returnVal = Double.valueOf(str);
        java.text.NumberFormat nf = java.text.NumberFormat.getInstance(java.util.Locale.ENGLISH);
        returnVal = Double.valueOf(nf.parse(str).doubleValue());
    } catch (ParseException e) {
    } catch (Exception e) {
    }
    return returnVal;
}

From source file:Main.java

public static String sGetDecimalStringAnyLocaleAs1Pt5LocalisedString(String value) {

    Locale theLocale = Locale.getDefault();

    NumberFormat numberFormat = DecimalFormat.getInstance(theLocale);
    Number theNumber;/*from   www  .  j a v a 2s.  c o  m*/
    try {
        theNumber = numberFormat.parse(value);
    } catch (ParseException e) {

        //value = sConvertDigits(value);
        String valueWithDot = value.replaceAll(",", ".");

        theNumber = Double.valueOf(valueWithDot);
    }

    // String.format uses the JVM's default locale.
    //String s = String.format(Locale.US, "%.2f", price);
    NumberFormat outputFormat = DecimalFormat.getInstance(theLocale);
    outputFormat.setMinimumIntegerDigits(1);
    outputFormat.setMinimumFractionDigits(5);
    outputFormat.setMaximumFractionDigits(5);
    String theStringResult = outputFormat.format(theNumber);
    //String theStringResult = String.format("%1.5f", theDoubleValue.doubleValue());

    return theStringResult;
}

From source file:Main.java

/**
 * Adopted from http://jgnash.svn.sourceforge.net/viewvc/jgnash/jgnash2/trunk/src/jgnash/imports/qif/QifUtils.java
 *//*from  www .j ava 2  s . c o  m*/
public static BigDecimal parseMoney(String money) {
    String sMoney = money;

    if (sMoney != null) {
        sMoney = sMoney.trim(); // to be safe
        try {
            return new BigDecimal(sMoney);
        } catch (NumberFormatException e) {
            /* there must be commas, etc in the number.  Need to look for them
             * and remove them first, and then try BigDecimal again.  If that
             * fails, then give up and use NumberFormat and scale it down
             * */
            String[] split = MONEY_PREFIX_PATTERN.split(sMoney);
            if (split.length >= 2) {
                StringBuilder buf = new StringBuilder();
                if (sMoney.startsWith("-")) {
                    buf.append('-');
                }
                for (int i = 0; i < split.length - 1; i++) {
                    buf.append(split[i]);
                }
                buf.append('.');
                buf.append(split[split.length - 1]);
                try {
                    return new BigDecimal(buf.toString());

                } catch (final NumberFormatException e2) {
                    Log.e("QifUtils", "Second parse attempt failed, falling back to rounding");
                }
            }
            NumberFormat formatter = NumberFormat.getNumberInstance();
            try {
                Number num = formatter.parse(sMoney);
                return new BigDecimal(num.floatValue());
            } catch (ParseException ignored) {
            }
            Log.e("QifUtils", "Could not parse money " + sMoney);
        }
    }
    return new BigDecimal(0);
}

From source file:Main.java

/**
 * Adopted from http://jgnash.svn.sourceforge.net/viewvc/jgnash/jgnash2/trunk/src/jgnash/imports/qif/QifUtils.java
 *///from w w w.ja  v  a  2 s.  c  o  m
public static long parseMoney(String money) {
    if (money != null) {
        BigDecimal bdMoney;
        money = money.trim(); // to be safe
        try {
            bdMoney = new BigDecimal(money);
            return moneyAsLong(bdMoney);
        } catch (NumberFormatException e) {
            /* there must be commas, etc in the number.  Need to look for them
             * and remove them first, and then try BigDecimal again.  If that
             * fails, then give up and use NumberFormat and scale it down
             * */
            String[] split = MONEY_PREFIX_PATTERN.split(money);
            if (split.length > 1) {
                StringBuilder buf = new StringBuilder();
                if (money.startsWith("-")) {
                    buf.append('-');
                }
                for (int i = 0; i < split.length - 1; i++) {
                    buf.append(split[i]);
                }
                buf.append('.');
                buf.append(split[split.length - 1]);
                try {
                    bdMoney = new BigDecimal(buf.toString());
                    return moneyAsLong(bdMoney);
                } catch (final NumberFormatException e2) {
                    Log.e("QifUtils", "Second parse attempt failed, falling back to rounding");
                }
            }
            NumberFormat formatter = NumberFormat.getNumberInstance();
            try {
                Number num = formatter.parse(money);
                BigDecimal bd = new BigDecimal(num.floatValue());
                if (bd.scale() > 6) {
                    bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
                }
                return moneyAsLong(bd);
            } catch (ParseException ignored) {
            }
            Log.e("QifUtils", "Could not parse money " + money);
        }
    }
    return 0;
}

From source file:Main.java

public static double getDoubleFromCurrencyString(String formattedString) {
    NumberFormat nf = NumberFormat.getCurrencyInstance();

    // really lazy this shouldn't blindly check for whether it is a currency or not..
    try {/*  w w  w  .ja va2  s  .  c om*/
        return nf.parse(formattedString).doubleValue();
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return getDoubleFrom(formattedString);
}