Java货币库 [英] Currency library for Java

查看:90
本文介绍了Java货币库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一些货币转换器库可以将值从特定货币转换为另一种货币?还是我应该为此实现我的how类?

Is there some currency converter library that enables to convert a value from a specific currency to another? Or should I implement my how class for this?

如果某人有某种示例,那就太好了……

If someone has some kind of example it would be great...

推荐答案

由于货币的动态性质,理想情况下,您不应该编写自己的公式来转换货币。最好访问一些可以可靠地用于货币转换的公共API。这样的API之一就是 Yahoo 货币转换器API。 Yahoo API非常简单。获取两种货币之间的当前货币汇率的基本基本要求如下:

Ideally you should not write your own formulas to convert the currency due to the dynamic nature of currencies. It will be a good idea to access some public APIs, which can be reliably used to do the currency conversion. One of such API is Yahoo currency convertor API. Yahoo API is very simple. The basic general request for getting the current currency rate between two currencies looks like:


http://download.finance.yahoo.com/d/quotes.csv?s= [来源货币] [至
货币] = X& f = l1& e = .cs

http://download.finance.yahoo.com/d/quotes.csv?s=[From Currency][To Currency]=X&f=l1&e=.cs

例如,为了获得当前美元与以色列谢克尔之间的汇率,应构建以下请求:

For example, in order to get the current currency rate between US Dollars and Israeli Shekels, the following request should be constructed:


http://download.finance.yahoo.com/d/quotes .csv?s = USDILS = X& f = l1& e = .cs

获取货币汇率信息是非常简单。它从定义基本转换器行为的基本接口开始:

Getting the currency rate information is pretty straight forward. It starts with a basic interface to define a general converter behavior:

public interface CurrencyConverter {
    public float convert(String currencyFrom, String currencyTo) throws Exception;
}

并且带有基本主应用程序的实现类显示其用法:

And the implementing class with a basic main application showing its usage:

import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

import java.io.IOException;

public class YahooCurrencyConverter implements CurrencyConverter
{
    public float getConversionRate(String from, String to) throws IOException
    {
        HttpClientBuilder builder = HttpClientBuilder.create();
        try (CloseableHttpClient httpclient = builder.build())
        {
            HttpGet httpGet = new HttpGet("http://quote.yahoo.com/d/quotes.csv?s=" + from + to + "=X&f=l1&e=.csv");
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String responseBody = httpclient.execute(httpGet, responseHandler);

            return Float.parseFloat(responseBody);
        }
    }

    public static void main(String[] arguments) throws IOException
    {
        YahooCurrencyConverter yahooCurrencyConverter = new YahooCurrencyConverter();
        float current = yahooCurrencyConverter.getConversionRate("USD", "ILS");
        System.out.println(current);
    }
}

重要提示:Yahoo或任何其他提供商除非您不付款,否则没有义务提供此类API。因此,如果您要根据它们构建商业应用程序,则可能需要寻找一些付费API。或者,您需要保持警惕,以确保免费的API能够正确启动并运行

这篇关于Java货币库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆