Javascript calculate quantity * price

爱⌒轻易说出口 提交于 2019-12-23 05:18:28

问题


I'm trying to multiply the quantity and the price for each item for calculating the total, but I'm getting an error in my alert.

$.each(data.items, function(index, d){
    var calcultest = d.price * d.qty;
    alert(calcultest)
});

回答1:


Use parseFloat to convert string to Float

$.each(data.items, function(index, d){
    var Price = d.price.replace(",",".");
    var calcultest = parseFloat(Price) * parseFloat(d.qty);
    alert(calcultest.toString().replace(".",","));
});

FIDDLE




回答2:


Can you try this,

 $.each(data.items, function(index, d){
        var Price = d.price;
        Price = Price.replace(",",".");
        var calcultest = parseInt(Price) * parseInt(d.qty);            
        alert(calcultest);
 });



回答3:


If you want to create simple calculators with Javascript I would recommend you to take a look at “Appizy”.

It converts instantly a spreadsheet into a standalone web calculation tool (HTML+CSS+JAVASCRIPT). You don’t need to hardcode the whole interface and calculation formulas!

Just create your working calculator with Open- or LibreOffice and then use Appizy.

I hope it can help. Best, Nicolas



来源:https://stackoverflow.com/questions/21075595/javascript-calculate-quantity-price

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!