Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript format currency

function formatToCurrency(amount){
    return (amount).toFixed(2).replace(/d(?=(d{3})+.)/g, '$&,'); 
}
formatToCurrency(12.34546); //"12.35"
formatToCurrency(42345255.356); //"42,345,255.36"
Comment

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 :: print json object 
Javascript :: paypal in react js 
Javascript :: version check 
Javascript :: ternary 
Javascript :: react-native-dropdown-picker for form react native 
Javascript :: rest parameters 
Javascript :: date difference without weekends using moment js 
Javascript :: javascript strings vs numbers 
Javascript :: google scripts string split 
Javascript :: how to update specific key of an object in reducer 
Javascript :: create slug using middleware 
Javascript :: javascript length of array 
Javascript :: javascript event listener get id of clicked items 
Javascript :: Fetching data error and pending load check 
Javascript :: hook usePreloadImages 
Javascript :: jquery steps disable finish button 
Javascript :: object method in javascript 
Javascript :: Stop modal from closing on outside click 
Javascript :: sort array ij js 
Javascript :: create expo project with a specific expo SDK 
Javascript :: deserialize json to c# object 
Javascript :: react google map api 
Javascript :: chess.js 
Javascript :: update node two versions mac 
Javascript :: nodejs stream 
Javascript :: es6 range 
Javascript :: how to print hello world in javascript 
Javascript :: animated node with tag 1 does not exist 
Javascript :: javascript sort array by column 
Javascript :: angular json and cli json file 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =