How to Convert a Price from USD to EUR in Java
Automatically convert the price in the source currency into the destination currency using the latest available currency exchange rate data.
Join the DZone community and get the full member experience.
Join For FreeOver the past year, we’ve seen an abundance of changes that influence the way we do business, and with the roll out of lightning-quick broadband and 5G as well, the world is more connected and reliant on the internet than ever. This global culture has led to more business with international clients via websites and programs, and converting prices to the proper currency is an integral piece of meeting standards across the board. However, between keeping up with exchange rates and processing manual calculations, price conversion has the potential to become quite the time-consuming task. If we want to ensure the most accurate and efficient user experience, automation can be a key addition to our approach.
Enter the Cloudmersive Currency Conversion API; this innovative solution can be utilized to simplify the currency conversion process by leveraging continuously updated currency exchange rate data to produce an instant result. The goal of this article is to walk you through how to install and use this feature in Java to simultaneously improve customer satisfaction and system processing by taking a price in any source currency and converting it to a destination currency.
Without further ado, let’s get started. To install the SDK with Maven, we will need to add a reference to the repository in pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Next, add a reference to the dependency:
xxxxxxxxxx
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.54</version>
</dependency>
</dependencies>
Now that we’ve finished installing the package, we can add the imports to the top of the file and use the following code to call the price conversion function:
xxxxxxxxxx
// 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();
}
To ensure the function runs properly, we will need to input the following parameters as well:
- API key – this can be retrieved by creating an account on the Cloudmersive website; registration is free and will give you access to 800 monthly calls across a multitude of APIs
- Source currency three-digit code (e.g. USD, EUR, etc.)
- Destination currency three-digit code (e.g. USD, EUR, etc.)
- Price (e.g. 19.99 in source currency)
This will allow us to provide a more integrated experience for international clients, and an easier business flow for ourselves.
If this API has been useful, you may be interested in additional solutions including obtaining a specific exchange rate between source and destination currencies, or retrieving a list of available currencies and corresponding countries. If you have questions, we’re here to help; simply contact our team via the Cloudmersive website, and we will be happy to assist.
Opinions expressed by DZone contributors are their own.
Comments