'parseFloat doesn't work and returns as NaN React
So, I'm trying to do some calculations from the field i have taken from an input:
const a = {
price: '12',
qta: '2',
unit: '2',
total: '44' // I want to calculate this as * and put it here
}
So the problem is I'm taking as strings and I have to convert to number, I do use parseFloat for this as the following example:
const unit = parseFloat(temp[_index_p].unit);
const hours = parseFloat(temp[_index_p].hours);
const price = parseFloat(temp[_index_p].price);
const newTotal = unit * price + hours * price;
temp[_index_p] = {
...temp[_index_p],
[property]: value,
total: newTotal,
};
}
setData({ ...data, parts: temp });
So that code is called on edit and the problem is something doesn't work and returns `NaN and not the wanted value.
// Ignore the index part because I'm editing multipy object, but consider it as one single object
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
