chooseMoneyData:{
act_fee: "0.01"
cs_fee: "0.04"
freight_fee: "10"
package_fee: "0.01"
}
changeOnePrice() {
let countPrice = 0;
const multiplier = 100; // 将金额转为以分为单位的倍数
// 将浮点数金额转换为以分为单位的整数
const priceInCents = Math.round(this.price * multiplier);
for (let key in this.chooseMoneyData) {
// 将浮点数金额转换为以分为单位的整数
const amountInCents = Math.round(Number(this.chooseMoneyData[key]) * multiplier);
countPrice += amountInCents;
}
// 总金额已经是以分为单位的整数,不再需要除以倍数
// this.onePrice = (priceInCents - countPrice).toFixed(2); // 将结果转为字符串表示,如果需要保留小数,请使用 toFixed(2)
// 总金额已经是以分为单位的整数,将结果转为元
const resultInYuan = (priceInCents - countPrice) / multiplier;
this.onePrice = resultInYuan.toFixed(2); // 将结果转为字符串表示,保留两位小数
},
// 元转为分
yuanToFen(price){
// 假设 Price 已经是以元为单位的字符串
const onePriceInYuan = parseFloat(price); // 转换为浮点数
const multiplier = 100; // 将金额转为以分为单位的倍数
// 将浮点数金额转换为以分为单位的整数
const onePriceInCents = Math.round(onePriceInYuan * multiplier);
// 将结果转为字符串表示,如果需要保留小数,请使用 toFixed(2)
return onePriceInCents.toFixed(0);
},
js金额浮点数的相加和计算,分转元
最新推荐文章于 2024-04-02 10:51:22 发布