1.3 的值在 parsefloat 中变为 1.40000001
value of 1.3 is becoming 1.40000001 in parsefloat
我正在从 mongodb 中读取一些值,然后我有一些可以提高价格的选项。有 2 种类型的选项单一和多个。 if single 所有操作运行 顺利。如果在我读取值时有多个(例如“1.3”)解析 float returns 1.4000001
执行此操作的代码如下:
this.item.productPrice = parseFloat(this.item.productPrice) -
parseFloat(a.price);
和
this.item.productPrice = parseFloat(this.item.productPrice) + parseFloat(a.price);
using debugger at the beginning of the line item.productPrice is 1.3 but after the parsing it become 1.4000001 and adds the a.price
完整代码块
//this one takes the selection and adds it to selectedparams array
selected(ev: any) {
debugger;
const a = JSON.parse(ev);
if (a.multiple) {
if (Array.isArray(this.selectedparams[a.paramName])) {
const va = !a.value;
const index = this.objectinArray(this.selectedparams[a.paramName], {
option: a.option,
price: a.price,
value: va
});
if (index !== -1) {
this.item.productPrice = parseFloat(this.item.productPrice) - parseFloat(a.price);
this.selectedparams[a.paramName].splice(index, 1);
this.zone.run(() => {});
} else {
this.selectedparams[a.paramName].push({ option: a.option, price: a.price, value: a.value });
this.item.productPrice = parseFloat(this.item.productPrice) + parseFloat(a.price);
this.zone.run(() => {});
}
} else {
this.selectedparams[a.paramName] = [];
this.selectedparams[a.paramName].push({ option: a.option, price: a.price, value: a.value });
this.item.productPrice = this.item.productPrice + parseFloat(a.price);
console.log('hereeeeeeeeeeeeeeeee', this.item.productPrice);
this.zone.run(() => {});
}
} else {
this.selectedparams[a.paramName] = { selected: a.selected, price: a.price };
}
this.calcPrice();
}
//this one dows the calculation after adding
calcPrice() {
const keys = Object.keys(this.selectedparams);
console.log(this.item);
let newprice = this.item.productPrice;
keys.forEach(el => {
if (Array.isArray(this.selectedparams[el])) {
this.selectedparams[el].forEach((i: any) => {
newprice = newprice + parseFloat(i.price);
});
} else {
newprice = newprice + parseFloat(this.selectedparams[el].price);
}
});
this.showPrice = newprice;
}
TLDR(math behind) - You could use the .toFixed()函数:
var result = parseFloat('0.1') + parseFloat('1.3');
console.log(result.toFixed(1)); // 1.4
var result = parseFloat('0.1') + parseFloat('1.3');
console.log(result); // 1.400000001
我正在从 mongodb 中读取一些值,然后我有一些可以提高价格的选项。有 2 种类型的选项单一和多个。 if single 所有操作运行 顺利。如果在我读取值时有多个(例如“1.3”)解析 float returns 1.4000001 执行此操作的代码如下:
this.item.productPrice = parseFloat(this.item.productPrice) -
parseFloat(a.price);
和
this.item.productPrice = parseFloat(this.item.productPrice) + parseFloat(a.price);
using debugger at the beginning of the line item.productPrice is 1.3 but after the parsing it become 1.4000001 and adds the a.price
完整代码块
//this one takes the selection and adds it to selectedparams array
selected(ev: any) {
debugger;
const a = JSON.parse(ev);
if (a.multiple) {
if (Array.isArray(this.selectedparams[a.paramName])) {
const va = !a.value;
const index = this.objectinArray(this.selectedparams[a.paramName], {
option: a.option,
price: a.price,
value: va
});
if (index !== -1) {
this.item.productPrice = parseFloat(this.item.productPrice) - parseFloat(a.price);
this.selectedparams[a.paramName].splice(index, 1);
this.zone.run(() => {});
} else {
this.selectedparams[a.paramName].push({ option: a.option, price: a.price, value: a.value });
this.item.productPrice = parseFloat(this.item.productPrice) + parseFloat(a.price);
this.zone.run(() => {});
}
} else {
this.selectedparams[a.paramName] = [];
this.selectedparams[a.paramName].push({ option: a.option, price: a.price, value: a.value });
this.item.productPrice = this.item.productPrice + parseFloat(a.price);
console.log('hereeeeeeeeeeeeeeeee', this.item.productPrice);
this.zone.run(() => {});
}
} else {
this.selectedparams[a.paramName] = { selected: a.selected, price: a.price };
}
this.calcPrice();
}
//this one dows the calculation after adding
calcPrice() {
const keys = Object.keys(this.selectedparams);
console.log(this.item);
let newprice = this.item.productPrice;
keys.forEach(el => {
if (Array.isArray(this.selectedparams[el])) {
this.selectedparams[el].forEach((i: any) => {
newprice = newprice + parseFloat(i.price);
});
} else {
newprice = newprice + parseFloat(this.selectedparams[el].price);
}
});
this.showPrice = newprice;
}
TLDR(math behind) - You could use the .toFixed()函数:
var result = parseFloat('0.1') + parseFloat('1.3');
console.log(result.toFixed(1)); // 1.4
var result = parseFloat('0.1') + parseFloat('1.3');
console.log(result); // 1.400000001