How to Convert a Price from a Source Currency into a Destination Currency using Java

While the world has experienced extensive globalization in the past few decades, currency still remains a static difference between many countries (at least, those not involved in the EU or other such unions). As such, converting prices between currencies remains a constant need — and it never hurts to build that type of service into your applications. Using our Currency Conversion API allows you to convert a sourcePrice from a source country (specified by its three-digit code, i.e., USD, EUR, etc.) into a destination currency automatically. The best part? You can use this API for free — all you need to do is register a free account on our website and use Java code examples provided below in this article to structure your API call. Your free account will supply a free-tier API key valid for 800 API calls per month with zero additional costs/commitments.

Let’s dive right in. Our first step is to install the currency API client; we can do so by adding a reference to our repository in pom.xml:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

After that, we can add a reference to the dependency in pom.xml, and installation is complete:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

Now we can call the function, beginning by adding the imports to the top of our file. After that, include your source & destination currency codes in their respective fields, then supply your input price (and don’t forget to copy in your API key where indicated in the code comments):

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.CurrencyExchangeApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
CurrencyExchangeApi apiInstance = new CurrencyExchangeApi();
String source = "source_example"; // String | Source currency three-digit code (ISO 4217), e.g. USD, EUR, etc.
String destination = "destination_example"; // String | Destination currency three-digit code (ISO 4217), e.g. USD, EUR, etc.
Double sourcePrice = 3.4D; // Double | Input price, such as 19.99 in source currency
try {
ConvertedCurrencyResult result = apiInstance.currencyExchangeConvertCurrency(source, destination, sourcePrice);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling CurrencyExchangeApi#currencyExchangeConvertCurrency");
e.printStackTrace();
}

After that, you’re all done — no more code required.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Cloudmersive

Cloudmersive

436 Followers

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.