Reputation: 39
I am fetching numbers from this api
let fetchRes = fetch("https://api.lunarcrush.com/v2?data=assets&key=n8dyddsipg5611qg6bst9&symbol=AVAX")
.then((res) => res.json())
.then((result) => {
console.log(result);
document.getElementById('result').innerHTML = result.data[0].market_cap;
})
.catch(error => {
console.log(error);
})
I want the numbers fetched (example- 7692083188) to be displayed in like 7.69B. I know this code for converting but how do I connect both codes to acheive my data displayed as 7.69B?
function convertToInternationalCurrencySystem (labelValue) {
// Nine Zeroes for Billions
return Math.abs(Number(labelValue)) >= 1.0e+9
? (Math.abs(Number(labelValue)) / 1.0e+9).toFixed(2) + "B"
// Six Zeroes for Millions
: Math.abs(Number(labelValue)) >= 1.0e+6
? (Math.abs(Number(labelValue)) / 1.0e+6).toFixed(2) + "M"
// Three Zeroes for Thousands
: Math.abs(Number(labelValue)) >= 1.0e+3
? (Math.abs(Number(labelValue)) / 1.0e+3).toFixed(2) + "K"
: Math.abs(Number(labelValue));
}
( convertToInternationalCurrencySystem (7692083188) );
Upvotes: 1
Views: 419
Reputation: 16311
You just need to set your #result
innerHTML to convertToInternationalCurrencySystem(result.data[0].market_cap)
by changing this line:
document.getElementById('result').innerHTML = result.data[0].market_cap;
To this:
document.getElementById('result').innerHTML = convertToInternationalCurrencySystem(result.data[0].market_cap);
Check and run the following Code Snippet for a practical example of the above:
function convertToInternationalCurrencySystem(labelValue) {
// Nine Zeroes for Billions
return Math.abs(Number(labelValue)) >= 1.0e+9
? (Math.abs(Number(labelValue)) / 1.0e+9).toFixed(2) + "B"
// Six Zeroes for Millions
: Math.abs(Number(labelValue)) >= 1.0e+6
? (Math.abs(Number(labelValue)) / 1.0e+6).toFixed(2) + "M"
// Three Zeroes for Thousands
: Math.abs(Number(labelValue)) >= 1.0e+3
? (Math.abs(Number(labelValue)) / 1.0e+3).toFixed(2) + "K"
: Math.abs(Number(labelValue));
}
let fetchRes = fetch("https://api.lunarcrush.com/v2?data=assets&key=n8dyddsipg5611qg6bst9&symbol=AVAX")
.then((res) => res.json())
.then((result) => {
document.getElementById('result').innerHTML = convertToInternationalCurrencySystem(result.data[0].market_cap);
})
.catch(error => {
console.log(error);
})
<h1 id="result"></h1>
Upvotes: 1