如何使用 Javascript 获得精确到便士的购物车结账价格?

发布于 2024-10-30 22:39:36 字数 870 浏览 5 评论 0原文

如何使用 Javascript 获得精确到便士的购物车结账价格?

现在,在进行了所有的试用轮等之后,我正在尝试。使用 15 种高产品/价格进行测试,我的价格上涨了 1.5 美分。

            for (var i = 0; i < Cookie.products.length; i++) {
            boolActive = Cookie.products[i].og_active;              
            if (boolActive)
            {
                itemPrice = Cookie.products[i].price;
                itemQty = Cookie.products[i].quantity;
                itemDiscountPercent = Cookie.products[i].discount_percent;

                subtotal = itemPrice * itemQty;
                priceDiscount = (subtotal * itemDiscountPercent);
                                    discountAmount += priceDiscount;
            }
        }
        if (!isNaN(discountAmount))
        { 
            var newCartTotal = (cartTotal - priceDiscount);
            alert("New Cart Total: " + newCartTotal);
        }

How do I get cart checkout price exact to the penny using Javascript?

Right now after taking out all of the trial .rounds etc I was trying.. I am coming up 1.5 cents too high using a high 15 products/prices to test.

            for (var i = 0; i < Cookie.products.length; i++) {
            boolActive = Cookie.products[i].og_active;              
            if (boolActive)
            {
                itemPrice = Cookie.products[i].price;
                itemQty = Cookie.products[i].quantity;
                itemDiscountPercent = Cookie.products[i].discount_percent;

                subtotal = itemPrice * itemQty;
                priceDiscount = (subtotal * itemDiscountPercent);
                                    discountAmount += priceDiscount;
            }
        }
        if (!isNaN(discountAmount))
        { 
            var newCartTotal = (cartTotal - priceDiscount);
            alert("New Cart Total: " + newCartTotal);
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晚雾 2024-11-06 22:39:36
var newCartTotal = (cartTotal - pricediscount).toFixed(2)

这将为您提供值,但它将是一个字符串。如果您需要它保持数字,请使用:

var newCartTotal = ((cartTotal - pricediscount * 100) << 0) / 100;
var newCartTotal = (cartTotal - pricediscount).toFixed(2)

that will give you the value, but it will be a string. If you need it to stay numeric, use:

var newCartTotal = ((cartTotal - pricediscount * 100) << 0) / 100;
断桥再见 2024-11-06 22:39:36

您需要对每个订单项的折扣进行四舍五入: priceDiscount = round_to_hundredth(subtotal * itemDiscountPercent)

请注意,如果您添加未四舍五入的结果,然后四舍五入,则此结果可能与您得到的结果不一致。和。但是,这是手动计算发票时通常的工作方式(特别是因为每个项目可以有不同的折扣百分比,所以折扣是针对每行计算的)。

我认为您遗漏了一行内容:discountAmount += PriceDiscount

You need to round the discount for each line item: priceDiscount = round_to_hundredth(subtotal * itemDiscountPercent)

Note that this result may not agree with the result you'd get if you add the unrounded results and then round the sum. However, this is the way invoices usually work when calculated by hand (especially since each item can have a different discount percent, so the discount is calculated for each line).

I think you left out a line saying discountAmount += priceDiscount.

燕归巢 2024-11-06 22:39:36

将您的代码修改为:

priceDiscount = parseFloat( (subtotal * itemDiscountPercent).toFixed(2) );

和:

newCartTotal = parseFloat( (cartTotal - priceDiscount).toFixed(2) );

modify your code to :

priceDiscount = parseFloat( (subtotal * itemDiscountPercent).toFixed(2) );

and:

newCartTotal = parseFloat( (cartTotal - priceDiscount).toFixed(2) );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文