Android Open Source - journal Currency Helper






From Project

Back to project page journal.

License

The source code is released under:

MIT License

If you think the Android project journal listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package cochrane343.journal;
/* w ww . j  ava  2 s.c  o  m*/
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.Locale;

/**
 * The helper class for handling amounts of money.
 *
 * @author cochrane34
 * @since 1.0
 */
public class CurrencyHelper {
    private static final String CURRENCY_INPUT_FORMAT = "%.2f";
    private static final String CURRENCY_INPUT_FORMAT_REGEX = "^[0-9]+([\\.][0-9]{1,2})?$";
    
    private static final BigDecimal CURRENCY_FRACTION_MULTIPLICAND = new BigDecimal(100);

    /**
     * @return the currency symbol for the current default locale, e.g. '$' for <code>Locale.US</code>
     */
    public static String getCurrencySymbol() {
        return NumberFormat.getCurrencyInstance().getCurrency().getSymbol();
    }
    
    /**
     * @param input the user input string
     * @return true if the input string represents a cent based amount of money or false otherwise
     */
    public static boolean isValidInput(final String input) {
        return input.matches(CURRENCY_INPUT_FORMAT_REGEX);
    }
    
    /**
     * @param input the user input string
     * @return the amount of money in cents represented by the input string
     */
    public static long parseInput(final String input) {
        final BigDecimal amount = new BigDecimal(input);
        final BigDecimal amountInCents = amount.multiply(CURRENCY_FRACTION_MULTIPLICAND);
 
        return amountInCents.longValue();
    }
    
    /**
     * @param amountInCents the amount in cents of money to print
     * @return the print string for the given amount for the current default locale, e.g. 
     * '120' is printed as '$1.20' for <code>Locale.US</code>
     */
    public static String toPrintString(final long amountInCents) {
        final BigDecimal amountInCentsDecimal = new BigDecimal(amountInCents);
        final BigDecimal amount = amountInCentsDecimal.divide(CURRENCY_FRACTION_MULTIPLICAND);

        final NumberFormat currenyFormat = NumberFormat.getCurrencyInstance();
        
        return currenyFormat.format(amount);
    }

    /**
     * @param amountInCents the amount in cents of money to format
     * @return a string representing an user input for the given amount of money, which
     * can be used as a default value for input fields
     */
    public static String toInputString(final long amountInCents) {
        final BigDecimal amountInCentsDecimal = new BigDecimal(amountInCents);
        final BigDecimal amount = amountInCentsDecimal.divide(CURRENCY_FRACTION_MULTIPLICAND);
        
        return String.format(Locale.ENGLISH, CURRENCY_INPUT_FORMAT, amount);
    }
}




Java Source Code List

cochrane343.journal.Constants.java
cochrane343.journal.CurrencyHelper.java
cochrane343.journal.DateTimeHelper.java
cochrane343.journal.ExpenseEditListener.java
cochrane343.journal.ExpensesListAdapter.java
cochrane343.journal.MainActivity.java
cochrane343.journal.MonthlyExpensesFragment.java
cochrane343.journal.MonthlyExpensesPagerAdapter.java
cochrane343.journal.SettingsActivity.java
cochrane343.journal.TranslationHelper.java
cochrane343.journal.contentprovider.JournalContentProvider.java
cochrane343.journal.contentprovider.JournalContract.java
cochrane343.journal.contentprovider.JournalDatabaseHelper.java
cochrane343.journal.dialogs.CategorySpinnerAdapter.java
cochrane343.journal.dialogs.ExpenseDialogFragment.java
cochrane343.journal.dialogs.ExpenseDialogListener.java
cochrane343.journal.exceptions.IllegalDisplayModeException.java
cochrane343.journal.exceptions.IllegalLoaderIdException.java
cochrane343.journal.exceptions.IllegalUriException.java
cochrane343.journal.exceptions.MissingFragmentArgumentException.java