React (JavaScript) 货币转换未返回正确价格
React (JavaScript) currency converting not returning correct price
这个项目本身正在使用 React。货币转换器显然是第三方 JavaScript 转换器,不在 React 中。
API 端点不包括加拿大价格,仅包括美国价格。 React 代码中的逻辑是将货币转换为加拿大货币,但加拿大的价格不正确——它低于应有的价格(它高于美国的价格,但从来没有这种情况)。另外,加拿大价格位于美国国旗旁边,不是加拿大国旗旁边的金额。
免责声明 - 这不是我的代码。我已经从一个不在这里的开发人员那里接管了它。没有文档。
Live link to a page with incorrect Canadian pricing. 您可以看到加拿大价格在美国国旗图标旁边的位置 - 但它比美国价格 34,000 美元低很多。自然,加拿大的价格应该更高。
我已将完整代码上传至 Github,可在 here.
找到
来自“HelperFunctions.ts”文件:
export function formatPrice(price, lang, inclCurTxt?: boolean, currency?: string) {
let formattedPrice = price;
const usaRate = .74;
if (lang === "fr") {
//FRENCH
const currencyText = (inclCurTxt ? " CA" : "");
if (currency != null && currency === "US") {
//USD
formattedPrice = accounting.formatMoney((Number(price) * usaRate), "", 0, " ") + " $" + currencyText;
} else {
//CAD
formattedPrice = accounting.formatMoney(price, "", 0, " ") + " $" + currencyText;
}
} else {//ENGLISH
const currencyText = (inclCurTxt ? " CAD" : "");
if (currency != null && currency === "US") {
//USD
formattedPrice = accounting.formatMoney((Number(price) * usaRate), "$", 0) + currencyText;
} else {
//CAD
formattedPrice = accounting.formatMoney(price, "$", 0) + currencyText;
}
}
return formattedPrice;
}
来自“MachineImagesAndInfo.tsx”文件:
//PRICE
if (
props.jsonDataProduct.price != null &&
props.jsonDataProduct.price.text != null
) {
detailsHtml.itemPriceCA = formatPrice(
props.jsonDataProduct.price.text,
props.lang
);
detailsHtml.itemPriceUS = formatPrice(
props.jsonDataProduct.price.text,
props.lang,
false,
"US"
);
}
如何正确转换加拿大价格并显示正确的价格?
似乎原始函数假设输入价格以CAD为单位,CADUSD的汇率被硬编码为0.74。该函数采用价格(假设它是 CAD)并将其乘以 0.74。根据当前逻辑,您应该将速率更新为 1.35 或将乘法切换为除法。
总的来说,我会推荐重构。对于一个非常简单的任务来说,该函数非常冗长。
这个项目本身正在使用 React。货币转换器显然是第三方 JavaScript 转换器,不在 React 中。
API 端点不包括加拿大价格,仅包括美国价格。 React 代码中的逻辑是将货币转换为加拿大货币,但加拿大的价格不正确——它低于应有的价格(它高于美国的价格,但从来没有这种情况)。另外,加拿大价格位于美国国旗旁边,不是加拿大国旗旁边的金额。
免责声明 - 这不是我的代码。我已经从一个不在这里的开发人员那里接管了它。没有文档。
Live link to a page with incorrect Canadian pricing. 您可以看到加拿大价格在美国国旗图标旁边的位置 - 但它比美国价格 34,000 美元低很多。自然,加拿大的价格应该更高。
我已将完整代码上传至 Github,可在 here.
找到来自“HelperFunctions.ts”文件:
export function formatPrice(price, lang, inclCurTxt?: boolean, currency?: string) {
let formattedPrice = price;
const usaRate = .74;
if (lang === "fr") {
//FRENCH
const currencyText = (inclCurTxt ? " CA" : "");
if (currency != null && currency === "US") {
//USD
formattedPrice = accounting.formatMoney((Number(price) * usaRate), "", 0, " ") + " $" + currencyText;
} else {
//CAD
formattedPrice = accounting.formatMoney(price, "", 0, " ") + " $" + currencyText;
}
} else {//ENGLISH
const currencyText = (inclCurTxt ? " CAD" : "");
if (currency != null && currency === "US") {
//USD
formattedPrice = accounting.formatMoney((Number(price) * usaRate), "$", 0) + currencyText;
} else {
//CAD
formattedPrice = accounting.formatMoney(price, "$", 0) + currencyText;
}
}
return formattedPrice;
}
来自“MachineImagesAndInfo.tsx”文件:
//PRICE
if (
props.jsonDataProduct.price != null &&
props.jsonDataProduct.price.text != null
) {
detailsHtml.itemPriceCA = formatPrice(
props.jsonDataProduct.price.text,
props.lang
);
detailsHtml.itemPriceUS = formatPrice(
props.jsonDataProduct.price.text,
props.lang,
false,
"US"
);
}
如何正确转换加拿大价格并显示正确的价格?
似乎原始函数假设输入价格以CAD为单位,CADUSD的汇率被硬编码为0.74。该函数采用价格(假设它是 CAD)并将其乘以 0.74。根据当前逻辑,您应该将速率更新为 1.35 或将乘法切换为除法。
总的来说,我会推荐重构。对于一个非常简单的任务来说,该函数非常冗长。