如何在结帐总额的末尾添加一个零?

我希望结帐总金额为零,因为在最后以零结尾的产品被添加到购物篮中,总金额显示为 1.5 英镑

那么如何才能改变它以像这样显示 1.50 英镑而不会弄乱其他产品呢?

任何代码片段将不胜感激。


芜湖不芜
浏览 98回答 1
1回答

忽然笑

total好吧,在您的代码末尾编辑此代码total.toFixed(2)编辑:在最后一行代码function updateCartTotal() {var cartItemContainer = document.getElementsByClassName('cart-items')[0]var cartRows = cartItemContainer.getElementsByClassName('cart-row')var total = 0for (var i = 0; i < cartRows.length; i++) {&nbsp; &nbsp; var cartRow = cartRows[i]&nbsp; &nbsp; var priceElement = cartRow.getElementsByClassName('cart-price')[0]&nbsp; &nbsp; var quantityElement = cartRow.getElementsByClassName('cart-quantity-input')[0]&nbsp; &nbsp; var price = parseFloat(priceElement.innerText.replace('£', ''))&nbsp; &nbsp; var quantity = quantityElement.value&nbsp; &nbsp; total = total + (price * quantity)}total = Math.round(total * 100) / 100document.getElementsByClassName('cart-total-price')[0].innerText = '£' + total.toFixed(2)}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript