Login or Register   |   My Cart   |   Help   |   中文   |   ¥RMB
Blog
Return to Wokai.org

Wokai Blog: Currency Exchange Rate Conversion - Hacking 4 Good: Wokai China Microfinance Blog

Categories



Top Tags:



Viewing Blog Post #2174     Return to Top   RSS Feed  
Submitted by Ben Benson on August 24, 2010  

One of the challenges to building a micro-loan lending website is managing multiple currencies. Wokai's loans are presently shown in USD during fundraising but the actual loan granted to the rural Chinese farmer is of course in RMB. On another day, I will broach the subject of managing these various asset types in Wokai's accounting system. For now though, I thought I'd share a very simple and useful API for performing currency conversions based on the current exchange rate.

Here's a complete Java code example for converting RMB to USD (though pretty much any form of currency is supported).

public static BigDecimal convertRMB2USD(BigDecimal amountRMB)
{
    try
    {
        URL url = new URL("http://www.exchangerate-api.com/cny/usd/" 
          + amountRMB 
          + "?k=" 
          + Configuration.getExchangeRateAPIKey());
 
        BufferedReader in = new BufferedReader(
            new InputStreamReader(url.openStream()));

        String answer = in.readLine();
        in.close();

        return new BigDecimal(answer);
    }
    catch (MalformedURLException mue)
    {
        logger_.error("exchange rate API URL invalid", mue);
    }
    catch (IOException ioe)
    {
        logger_.error("exchange rate API I/O failure", ioe);
    }

    return null;
}
Viewing Blog Post #2174   RSS Feed  

Login to add your comment!

In the Media