Skip to content

Commit

Permalink
added parseFromISO87String and toISO87String
Browse files Browse the repository at this point in the history
suggested in jpos-users by Bjarne Henriksen - THANK YOU!
  • Loading branch information
ar committed Feb 28, 2014
1 parent 8be56f9 commit e67d4af
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
16 changes: 16 additions & 0 deletions jpos/src/main/java/org/jpos/iso/ISOCurrency.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.*;

/**
Expand Down Expand Up @@ -88,12 +89,27 @@ public static void loadPropertiesFromClasspath(String base)
* @param currency - The ISO currency to be converted (eg. ISOField 49)
* @return result - A double representing the converted field
* @throws IllegalArgumentException if we fail to convert the amount
* @deprecated You should never use doubles
*/
public static double convertFromIsoMsg(String isoamount, String currency) throws IllegalArgumentException
{
Currency c = findCurrency(currency);
return c.parseAmountFromISOMsg(isoamount);
}
public static String toISO87String (BigDecimal amount, String currency)
{
try {
Currency c = findCurrency(currency);
return ISOUtil.zeropad(amount.movePointRight(c.getDecimals()).setScale(0).toPlainString(), 12);
}
catch (ISOException e) {
throw new IllegalArgumentException("Failed to convert amount",e);
}
}
public static BigDecimal parseFromISO87String (String isoamount, String currency) {
int decimals = findCurrency(currency).getDecimals();
return new BigDecimal(isoamount).movePointLeft(decimals);
}

public static void addBundle(ResourceBundle r)
{
Expand Down
41 changes: 41 additions & 0 deletions jpos/src/test/java/org/jpos/util/ISOCurrencyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* jPOS Project [http://jpos.org]
* Copyright (C) 2000-2014 Alejandro P. Revilla
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.jpos.util;

import org.jpos.iso.ISOCurrency;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

import java.math.BigDecimal;

@SuppressWarnings("unused")
public class ISOCurrencyTest {
@Test
public void testparseFromISO87String () {
assertEquals("2 decimals", new BigDecimal("12.34"), ISOCurrency.parseFromISO87String("000000001234", "840"));
assertEquals("3 decimals", new BigDecimal("1.234"), ISOCurrency.parseFromISO87String("000000001234", "048"));
assertEquals("no decimals", new BigDecimal("1234"), ISOCurrency.parseFromISO87String("000000001234", "020"));
}
@Test
public void testtoISO87String () {
assertEquals("2 decimals", "000000001234", ISOCurrency.toISO87String(new BigDecimal("12.34"), "840"));
assertEquals("3 decimals", "000000001234", ISOCurrency.toISO87String(new BigDecimal("1.234"), "048"));
assertEquals("no decimals", "000000001234", ISOCurrency.toISO87String(new BigDecimal("1234"), "020"));
}
}

0 comments on commit e67d4af

Please sign in to comment.