Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to perform Currency Conversion
Currency conversion is a common programming task that involves multiplying an amount by an exchange rate. In C#, we can create simple currency conversion programs using basic arithmetic operations.
A currency converter takes an amount in one currency and converts it to another currency using the current exchange rate. The formula is: Converted Amount = Original Amount × Exchange Rate.
Syntax
Following is the basic syntax for currency conversion −
double convertedAmount = originalAmount * exchangeRate;
Where variables are declared as −
double originalAmount, convertedAmount, exchangeRate;
Using Simple Currency Conversion
Let's convert 10 US dollars to Indian Rupees (INR) using a fixed exchange rate −
using System;
public class Program {
public static void Main(string[] args) {
double usd, inr, exchangeRate;
// Amount in dollars
usd = 10;
// Current exchange rate (1 USD = 83 INR approximately)
exchangeRate = 83;
// Convert to INR
inr = usd * exchangeRate;
Console.WriteLine("{0} USD = {1} INR", usd, inr);
}
}
The output of the above code is −
10 USD = 830 INR
Using Multiple Currency Conversions
This example shows conversion between multiple currencies using different exchange rates −
using System;
public class Program {
public static void Main(string[] args) {
double amount = 100;
// Exchange rates (approximate values)
double usdToInr = 83.0;
double usdToEur = 0.92;
double usdToGbp = 0.79;
// Convert USD to other currencies
double inr = amount * usdToInr;
double eur = amount * usdToEur;
double gbp = amount * usdToGbp;
Console.WriteLine("Currency Conversion for {0} USD:", amount);
Console.WriteLine("INR: {0}", inr);
Console.WriteLine("EUR: {0}", eur);
Console.WriteLine("GBP: {0}", gbp);
}
}
The output of the above code is −
Currency Conversion for 100 USD: INR: 8300 EUR: 92 GBP: 79
Using Method-Based Currency Converter
A more organized approach using methods for different currency conversions −
using System;
public class CurrencyConverter {
public static double UsdToInr(double usd) {
return usd * 83.0;
}
public static double UsdToEur(double usd) {
return usd * 0.92;
}
public static double InrToUsd(double inr) {
return inr / 83.0;
}
}
public class Program {
public static void Main(string[] args) {
double amount = 50;
Console.WriteLine("Original amount: {0} USD", amount);
Console.WriteLine("Converted to INR: {0}", CurrencyConverter.UsdToInr(amount));
Console.WriteLine("Converted to EUR: {0:F2}", CurrencyConverter.UsdToEur(amount));
double inrAmount = 4150;
Console.WriteLine("\nOriginal amount: {0} INR", inrAmount);
Console.WriteLine("Converted to USD: {0:F2}", CurrencyConverter.InrToUsd(inrAmount));
}
}
The output of the above code is −
Original amount: 50 USD Converted to INR: 4150 Converted to EUR: 46.00 Original amount: 4150 INR Converted to USD: 50.00
Key Points
-
Always use
doubledata type for currency values to handle decimal amounts. -
Exchange rates fluctuate frequently, so use updated rates for accurate conversions.
-
Use
String.Format()or format specifiers like{0:F2}to limit decimal places. -
Consider creating separate methods or classes for better code organization.
Conclusion
Currency conversion in C# involves simple multiplication using exchange rates. For real-world applications, consider using APIs for live exchange rates and proper error handling for invalid inputs.
