Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

string to currency javascript

const money = 10000;
const currency = function(number){
    return new Intl.NumberFormat('en-IN', {style: 'currency',currency: 'INR', minimumFractionDigits: 2}).format(number);
};
console.log(currency(money));
Comment

JS Currency Converter

// include api for currency change
const api = "https://api.exchangerate-api.com/v4/latest/USD";
  
// for selecting different controls
var search = document.querySelector(".searchBox");
var convert = document.querySelector(".convert");
var fromCurrecy = document.querySelector(".from");
var toCurrecy = document.querySelector(".to");
var finalValue = document.querySelector(".finalValue");
var finalAmount = document.getElementById("finalAmount");
var resultFrom;
var resultTo;
var searchValue;
  
// Event when currency is changed
fromCurrecy.addEventListener('change', (event) => {
    resultFrom = `${event.target.value}`;
});
  
// Event when currency is changed
toCurrecy.addEventListener('change', (event) => {
    resultTo = `${event.target.value}`;
});
  
search.addEventListener('input', updateValue);
  
// function for updating value
function updateValue(e) {
    searchValue = e.target.value;
}
  
// when user clicks, it calls function getresults 
convert.addEventListener("click", getResults);
  
// function getresults
function getResults() {
    fetch(`${api}`)
        .then(currency => {
            return currency.json();
        }).then(displayResults);
}
  
// display results after convertion
function displayResults(currency) {
    let fromRate = currency.rates[resultFrom];
    let toRate = currency.rates[resultTo];
    finalValue.innerHTML = 
       ((toRate / fromRate) * searchValue).toFixed(2);
    finalAmount.style.display = "block";
}
  
// when user click on reset button
function clearVal() {
    window.location.reload();
    document.getElementsByClassName("finalValue").innerHTML = "";
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript change font color based on value 
Javascript :: strict mode in javascript 
Javascript :: check how many files in a folder js 
Javascript :: JavaScript read as Json 
Javascript :: do while javascript 
Javascript :: jquery check if element is hidden 
Javascript :: delete a node in dom javascript 
Javascript :: (Unauthorized) not authorized on admin to execute command 
Javascript :: javascript async function 
Javascript :: string to json nodejs 
Javascript :: javascript join array 
Javascript :: password confirmation using Joi 
Javascript :: next router 
Javascript :: jquery change position animate 
Javascript :: CREATE A BUTTON THAT INCREMENTS A COUNTER WHEN CLICKED 
Javascript :: radio button group get value javascript 
Javascript :: get date one week from now javascript 
Javascript :: add table row jquery 
Javascript :: js html tag valu 
Javascript :: settimeout in loop javascript 
Javascript :: javascript tofixed no trailing zeros 
Javascript :: npm install nodemon 
Javascript :: angular is not defined In AngularJS 
Javascript :: .NET number values such as positive and negative infinity cannot be written as valid JSON. 
Javascript :: dynamodb get all items nodejs 
Javascript :: confirm before close modal 
Javascript :: how to empty a filled input in cypress 
Javascript :: reset select option jquery 
Javascript :: indexof method javascript 
Javascript :: js add animation to element 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =