Hey!
I am using the code for the currency converter from here. https://www.wix.com/velo/example/currency-converter
To display the conversion result, a lot of characters after the comma are used.
How can you limit the output of characters to the third decimal place?
import { currencies } from 'wix-pay';
$w.onReady(function () {
initDropdowns();
$w('#convertButton').onClick(() => conversionHandler());
});
async function initDropdowns() {
const listOfAllCurrencies = await currencies.getAllCurrencies();
const optionsArray = formAsDropdownOptions(listOfAllCurrencies);
optionsArray.sort((option1, option2) => compareOptionsAlphabetically(option1.label, option2.label));
$w('#dropdownFrom').options = optionsArray;
$w('#dropdownTo').options = optionsArray;
}
function formAsDropdownOptions(listOfAllCurrencies) {
const optionsArray = listOfAllCurrencies.map((currencyItem) => {
return {
'value': currencyItem.code,
'label': currencyItem.code,
}
});
return optionsArray;
}
function compareOptionsAlphabetically(label1, label2) {
if (label1 > label2) {
return 1;
}
return -1;
}
async function conversionHandler() {
const amount = Number($w('#amountInput').value);
const fromCurrency = $w('#dropdownFrom').value;
const toCurrency = $w('#dropdownTo').value;
const convertionResult = await currencies.currencyConverter.convertAmounts({
'amounts': [amount],
'from': fromCurrency,
'to': toCurrency
});
const result = String((convertionResult.amounts[0]));
const timestamp = String(convertionResult.timestamp);
$w('#resultInput').value = result;
$w('#timestampText').text = `Rates are valid to \n ${timestamp}`;
$w('#timestampText').expand();
}