Java Currency Format toCurrency(Object objectValue)

Here you can find the source of toCurrency(Object objectValue)

Description

Parse double value to currency format (default Locale("es", "ES"))

License

Open Source License

Parameter

Parameter Description
objectValue a parameter

Declaration

public static String toCurrency(Object objectValue) 

Method Source Code

//package com.java2s;
/*//  ww w  .j ava 2 s . co m
 * Copyright (C) 2009-2015 SM2 SOFTWARE & SERVICES MANAGEMENT
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program has been created in the hope that it will be useful.
 * It is distributed WITHOUT ANY WARRANTY of any Kind,
 * without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 *
 * For more information, please contact SM2 Software & Services Management.
 * Mail: info@talaia-openppm.com
 * Web: http://www.talaia-openppm.com
 *
 * Module: utils
 * File: ValidateUtil.java
 * Create User: javier.hernandez
 * Create Date: 06/03/2015 14:35:37
 */

import java.text.NumberFormat;
import java.util.*;

public class Main {
    public static Locale DEF_LOCALE_NUMBER = new Locale("es", "ES");

    /**
     * Parse double value to currency format (default Locale("es", "ES"))
     * @param objectValue
     * @return
     */
    public static String toCurrency(Object objectValue) {

        return toCurrency(objectValue, DEF_LOCALE_NUMBER);
    }

    /**
     * Parse double value to currency format
     * @param objectValue
     * @return
     */
    public static String toCurrency(Object objectValue, Locale locale) {

        String value = "";
        double doubleValue = 0;

        if (objectValue instanceof Double) {
            doubleValue = (Double) objectValue;
        } else if (objectValue instanceof String) {
            doubleValue = Double.parseDouble((String) objectValue);
        }

        if (objectValue == null) {
            value = "";
        } else {
            NumberFormat numberFormat = NumberFormat.getNumberInstance(locale);
            numberFormat.setMinimumFractionDigits(2);
            numberFormat.setMaximumFractionDigits(2);

            value = numberFormat.format(doubleValue);
        }

        return value;
    }
}

Related

  1. getCurrencyFormat(final Locale locale)
  2. getCurrencyFormatter()
  3. parseCurrency(String currency)
  4. removeTrailingZeroes(BigDecimal value, boolean isCurrency)
  5. roundToCurrencyString(double amount)
  6. toCurrencyAmountStr(Long microAmount)
  7. toCurrencyFormat(String input)
  8. toCurrencyFormat(String pattern, double value)
  9. toCurrencyWord(Double doubleValue, Locale locale)