Java 的货币库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16999006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:39:29  来源:igfitidea点击:

Currency library for Java

javacurrency

提问by amp

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?

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

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

如果有人有某种例子,那就太好了......

回答by Juned Ahsan

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 Yahoocurrency convertor API. Yahoo API is very simple. The basic general request for getting the current currency rate between two currencies looks like:

理想情况下,由于货币的动态特性,您不应编写自己的公式来转换货币。访问一些公共 API 将是一个好主意,这些 API 可以可靠地用于进行货币转换。其中一种 API 是Yahoo货币转换器 API。雅虎 API 非常简单。获取两种货币之间当前货币汇率的基本一般请求如下所示:

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

http://download.finance.yahoo.com/d/quotes.csv?s=[从货币][到货币]=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

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);
    }
}

IMPORTANT: Yahoo or any other provider is not obliged to provide such APIs unless you are not paying them. So you may need to look for some paid APIs in case you are building a commercial applications depending on them. Or you need to be vigil to be sure that free APIs are UP and RUNNING properly for you

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

回答by Alexis C.

May you could take a look at the Currency Converterlibrary.

可以看看货币转换器库。

Currency Converter is a Java library which provides API for currency conversion. It uses different bank web-services to fetch rates and provides a single programming interface for any of supported web-services.

Currently these web-services are supported:

  • bank-ua.com
  • GoogleFinance web-service is under development for now

货币转换器是一个 Java 库,提供货币转换的 API。它使用不同的银行网络服务来获取利率,并为任何受支持的网络服务提供单一的编程接口。

目前支持这些网络服务:

  • 银行-ua.com
  • GoogleFinance 网络服务目前正在开发中

Example taken :

采取的例子:

// create an instance where USD is a default currency to convert from, and EUR a default one to convert to
// Using one of the implementation: BankUaCom
CurrencyConverter currencyConverter = new BankUaCom(Currency.USD, Currency.EUR);

// convert USD to EUR (the first parameter is amount of money you'd like to convert)
currencyConverter.convertCurrency(1f);

// the same
currencyConverter.convertCurrency(1f, Currency.EUR);

// the same
currencyConverter.convertCurrency(1f, Currency.USD, Currency.EUR);

// convert EUR to USD
currencyConverter.convertCurrency(1f, Currency.EUR, Currency.USD);

// and you can continue with any other supported currencies...

回答by knapcio

You want to convert it online or entering specific values previously? Maybe this will help: http://www.panticz.de/Simple-Java-currency-converter

您想在线转换它还是以前输入特定值?也许这会有所帮助:http: //www.panticz.de/Simple-Java-currency-converter