Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright (c) 2011 Denis Solonenko.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 */

import android.util.Log;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.ParseException;

import java.util.regex.Pattern;

public class Main {
    private static final Pattern MONEY_PREFIX_PATTERN = Pattern.compile("\\D");
    private static final BigDecimal HUNDRED = new BigDecimal(100);

    /**
     * Adopted from http://jgnash.svn.sourceforge.net/viewvc/jgnash/jgnash2/trunk/src/jgnash/imports/qif/QifUtils.java
     */
    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;
    }

    private static long moneyAsLong(BigDecimal bd) {
        return bd.multiply(HUNDRED).intValue();
    }
}