为什么这会以浮动小数值的形式返回?

时间:2015-08-03 22:31:22

标签: javascript

最后一行显示“总计为41.300000000000004”。我一直在repl.it中运行它。

var groceryList = [
    {food: "Mangos", price: 3.56},
    {food: "Grapes", price: 4.23},
    {food: "Chicken", price: 10.98},
    {food: "Sweet Potatoes", price: 7.94},
    {food: "Asst. Nuts", price: 6.54},
    {food: "Yogurt", price: 5.32},
    {food: "Papya", price: 2.73}
    ];

function shopping(items){
    console.log (items.food + " = " + items.price + ".");
    }
var grocery;
var total=0;
for ( grocery in  groceryList){
     
    total=total+groceryList[grocery].price;
    shopping(groceryList[grocery]);
}

    console.log("The grand total is " + total);

0 个答案:

没有答案