How can I convert a string of a number such as "131.40"
to the number 131.40
, or "14.1"
to the number 14.10
?
If I use parseFloat, the output leaves out the zero (131.4
) and toFixed returns a string. I'm trying to represent USD currency.
Here is the full context of my code:
const products = [{
id: 1,
price: 109.1,
quantity: 1,
title: "T-Shirt"
},
{
id: 2,
price: 22.3,
quantity: 1,
title: "Jeans"
}]
function totalPrice(storageItems){
const totalPriceHolder = []
storageItems.map((a, index) => {
const totalPrice = a.price * a.quantity;
totalPriceHolder.push((totalPrice))
})
console.log("Check1", totalPriceHolder)
const totalPriceReduce = totalPriceHolder.reduce((a, b) => a + b, 0).toFixed(2);
console.log("Check 2", totalPriceReduce)
return parseFloat(totalPriceReduce)
}
console.log(totalPrice(products))