001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.iso;
020
021import java.io.Serializable;
022
023
024/**
025 * ISO Currency Conversion package
026 *
027 * @author salaman@teknos.com
028 * @version $Id$
029 */
030public class Currency implements Serializable
031{
032    /** ISO alpha currency code (e.g. "USD"). */
033    String alphacode;
034    /** ISO numeric currency code. */
035    int isocode;
036    /** Number of decimal places for this currency. */
037    int numdecimals;
038
039    /**
040     * Creates a Currency with the given code and decimal count.
041     * @param alphacode ISO alpha code (e.g. "USD")
042     * @param isocode ISO numeric code
043     * @param numdecimals number of decimal places
044     */
045    public Currency(String alphacode, int isocode, int numdecimals)
046    {
047        this.alphacode = alphacode;
048        this.isocode = isocode;
049        this.numdecimals = numdecimals;
050    }
051
052    /**
053     * Returns the number of decimal places for this currency.
054     * @return decimal count
055     */
056    public int getDecimals()
057    {
058        return numdecimals;
059    }
060
061    /**
062     * Returns the ISO numeric currency code.
063     * @return ISO numeric code
064     */
065    public int getIsoCode()
066    {
067        return isocode;
068    }
069
070    /**
071     * Returns the ISO alpha currency code.
072     * @return alpha code (e.g. "USD")
073     */
074    public String getAlphaCode()
075    {
076        return alphacode;
077    }
078
079    /**
080     * Formats an amount for inclusion in an ISO message (zero-padded, 12 digits).
081     * @param amount the amount to format
082     * @return 12-character zero-padded string
083     */
084    public String formatAmountForISOMsg(double amount)
085    {
086        try
087        {
088            double m = Math.pow(10, getDecimals()) * amount;
089            return ISOUtil.zeropad(String.valueOf(Math.round(m)), 12);
090        }
091        catch (ISOException e)
092        {
093            throw new IllegalArgumentException("Failed to convert amount",e);
094        }
095    }
096
097    /**
098     * Parses an ISO amount string into a double by applying the currency's decimal shift.
099     * @param isoamount the ISO-formatted amount string (no decimal point)
100     * @return the decimal amount value
101     */
102    public double parseAmountFromISOMsg(String isoamount)
103    {
104        return Double.parseDouble(isoamount)/Math.pow(10, getDecimals());
105    }
106
107    @Override
108    public String toString()
109    {
110        return "Currency{" +
111               "alphacode='" + alphacode + '\'' +
112               ", isocode=" + isocode +
113               ", numdecimals=" + numdecimals +
114               '}';
115    }
116}