How to Create a Currency Converter in JavaScript

How to Create a Currency Converter in JavaScript

Introduction

In this tutorial we will create a How to Create a Currency Converter. This tutorial purpose is to teach you how to make a very simple currency converter. This will cover all the basic function that will convert any currency. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system that needed to convert any available currency. I will give my best to provide you the easiest way of creating this program Currency Converter. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will only display texboxes and buttton. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  5.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6.         </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">How to Create a Currency Converter</h3>
  15.                 <hr style="border-top:1px dotted #ccc;">
  16.                 <div class="col-md-4">
  17.                         <div class="form-group">
  18.                                 <label>Currency</label>
  19.                                 <select class="form-control" id="currency">
  20.                                         <option value="Euro">Euro</option>
  21.                                         <option value="Yen">Yen</option>
  22.                                         <option value="Yuan">Yuan</option>
  23.                                         <option value="Dollar">Dollar</option>
  24.                                         <option value="Pound">Pound</option>
  25.                                 </select>
  26.                         </div>
  27.                         <div class="form-group">
  28.                                 <input type="number" class="form-control" placeholder="Enter amount" id="amount"/>
  29.                         </div> 
  30.                                 <button class="btn btn-primary btn-block" style=" margin-top:20px;"onclick="convert();">Convert</button>
  31.                 </div>
  32.                 <div class="col-md-8">
  33.                         <div class="form-group">
  34.                                 <label>Converted</label>
  35.                                 <div style="width:100; height:100px; border:1px solid #000; background-color:gray; color:#fff; padding:20px; margin:10px;">
  36.                                         <span id="result" style="font-size:40px;"></span>
  37.                                 </div>
  38.                         </div>
  39.                 </div>
  40.         </div>
  41. </body>
  42. <script src="script.js"></script>
  43. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will convert the currency you use into a new currency value. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function convert(){    
  2.         let amount=document.getElementById("amount").value;
  3.         let currency=document.getElementById("currency").value;
  4.         if(amount == ""){
  5.                 alert('Please enter something first');
  6.         }else{
  7.                 switch(currency){
  8.                         case "Dollar":
  9.                                 let a_dollar=amount * 51.89;
  10.                                 let b_dollar=a_dollar.toFixed(2);
  11.                                 document.getElementById("result").innerHTML = "&#8369 "+ b_dollar;
  12.                         break;
  13.                        
  14.                         case "Pound":
  15.                                 let a_pound=amount * 72.39;
  16.                                 let b_pound=a_pound.toFixed(2);
  17.                                 document.getElementById("result").innerHTML = "&#8369 "+ b_pound;
  18.                         break;
  19.                        
  20.                         case "Euro":
  21.                                 let a_euro=amount * 63.84;
  22.                                 let b_euro=a_euro.toFixed(2);
  23.                                 document.getElementById("result").innerHTML = "&#8369 "+ b_euro;
  24.                         break;
  25.                        
  26.                         case "Yen":
  27.                                 let a_yen=amount * 0.49;
  28.                                 let b_yen=a_yen.toFixed(2);
  29.                                 document.getElementById("result").innerHTML = "&#8369 "+ b_yen;
  30.                         break;
  31.                        
  32.                         case "Yuan":
  33.                                 let a_yuan=amount * 8.20;
  34.                                 let b_yuan=a_yuan.toFixed(2);
  35.                                 document.getElementById("result").innerHTML = "&#8369 "+ b_yuan;
  36.                         break;
  37.                 }
  38.         }
  39. }

The code above is simple we just create only one method and we will call it convert(). This function will convert the currency you choose into a new currency, in my case I use philippine peso as a sample converted currency. We use case statement in order to handle the select tag option for several currency. In order to get your accurate converted currency you need to look for the current value for your currency value in the stock exchange market.

Output:

The How to Create a Currency Converter in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Create a Currency Converter in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment