So I’m trying to format data pulled from an API. The data is pulling and displaying just fine but I’d like to change the way it’s displayed. I know this topic has been covered before but it mostly relates to pulling and formatting data from a DB.
and this is the code below that I think I’m supposed to use to format the amount I want in the correct currency, although I’m not sure 100% sure as to where I should place it.
function currencyFormat(num) {
return '$' + num.toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,')
}
Thanks for the advice I went ahead and looked at the articles you sent and they were very helpful. I’m still struggling though in terms of locating the code snippet in the correct place. Below I’ve placed the new code begining with “var” and the console is logging the format correctly if I place dummy numbers there.
I’m not sure how to attach the particular line I want(price per coin, market cap) as the variable to format correctly.
import { getCryptoCurrencyInfo } from 'backend/serviceModule';
export function button1_click(event) {
//Add your code for this event here:
console.log("Retrieving information for " + $w("#currencyInput").value);
$w("#result").text = "Retrieving information for " + $w("#currencyInput").value;
getCryptoCurrencyInfo($w("#currencyInput").value)
.then(currencyInfo => {
var number = // what goes here to make my below, price per coin and market cap to format correctly
console.log(number.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2, style: 'currency', currency: 'USD' }))
$w("#result").text = "Name: " + currencyInfo[0].name + "\n" +
"Symbol: " + currencyInfo[0].symbol + "\n" +
"Rank: " + currencyInfo[0].rank + "\n" +
"Price Per Coin: " + currencyInfo[0].price_usd + "\n" +
"Market Capitalization: " + currencyInfo[0].market_cap_usd + "\n" +
"Percent Change - 1 hour: " + currencyInfo[0].percent_change_1h + "\n" +
"Percent Change - 1 Day: " + currencyInfo[0].percent_change_24h + "\n" +
"Percent Change - 1 Week: " + currencyInfo[0].percent_change_7d;
});
}