[{ code: "USD", name: "美元" },{ code: "EUR", name: "欧元" },{ code: "JPY", name: "日元" },{ code: "GBP", name: "英镑" },{ code: "CAD", name: "加元" },{ code: "AUD", name: "澳元" },{ code: "HKD", name: "港元" },{ code: "CHF", name: "瑞郎" },{ code: "SGD", name: "新元" },{ code: "NZD", name: "新西兰元" },{ code: "CNY", name: "人民币" },
]/*** 将货币金额转换为目标货币金额* @param {number} amount - 货币金额* @param {string} fromCurrency - 源货币种类,例如:USD* @param {string} toCurrency - 目标货币种类,例如:CNY* @return {Promise<number>} 返回对应的目标货币金额*/async convertCurrency(amount, fromCurrency, toCurrency) {// 定义汇率 API 地址const api = `/${fromCurrency}`;// 调用 API,得到汇率数据,并转换为 JSON 格式const response = await fetch(api);const data = await response.json();// 从汇率数据中获取源货币对目标货币的汇率const exchangeRate = data.rates[toCurrency];// 计算目标货币金额,保留两位小数const result = (amount * exchangeRate).toFixed(2);// 返回目标货币金额return result;},
本文发布于:2024-01-31 00:20:19,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170663162023886.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |