简体   繁体   English

如何用逗号计算金额

[英]How to calculate an amount with commas

I have a product that cost 1,500.85 and i want to multiply it with the quantity input using javascript. 我有一个商品价格为1,500.85,我想将其乘以使用javascript输入的数量。 But because the amount is over a thousand there is a comma and because of that comma the result shown "NaN" .. How do i calculate the amount with the comma? 但是,因为金额超过一千,所以有一个逗号,并且由于该逗号,结果显示为“ NaN” ..我如何用逗号计算金额?

NOTE: It works if the amount is under a thousand. 注意:如果金额少于一千,它将起作用。

 // Calculation script START $(document).ready(function() { CalculateTotalPrice(); }); document.getElementsByClassName("input-text qty text")[0].onkeyup = function() {CalculateTotalPrice()}; $(select).onchange(function() { CalculateTotalPrice(); }); function CalculateTotalPrice() { setTimeout(function(){ var price = document.querySelector(".price .woocommerce-Price-amount.amount").innerText; var quantity = document.getElementsByClassName("input-text qty text")[0].value; var total = price * quantity; var totalOnly2Decimal = total.toFixed(2); document.getElementById("result").innerHTML = "DKK " + totalOnly2Decimal + " inkl. moms"; }, 100); } // Calculation script END 
 <!-- Price --> <div class="elementor-widget-container"> <p class="price">Fra: <span class="woocommerce-Price-amount amount"> <span class="woocommerce-Price-currencySymbol"></span>1,122.50 </span> kr. inkl. moms</p> </div> <!-- Quantity field --> <div class="quantity"> <label class="screen-reader-text" for="quantity_5cd3fab7bb0d7"></label> <input type="number" id="quantity_5cd3fab7bb0d7" class="input-text qty text" step="1" min="1" max="" name="quantity" value="1" title="Stk." size="4" inputmode=""> <!-- Result --> <h3 style="font-size: 17px; font-weight:bold; display:inline; text-transform:uppercase;">Total:</h3> <p class="result" id="result" style="display:inline;"></p> 

Replace all the commas using String.prototype.replace then multiply. 使用String.prototype.replace替换所有逗号,然后相乘。

 let price = '1,500.85'; let quantity = '7'; let total = price.trim().replace(/,/g, '') * quantity; console.log(total) 

You can remove the comma with the replace method on the String object and convert to integer 您可以使用String对象上的replace方法删除逗号并将其转换为整数

price = parseInt(price.replace(/,/g,''))
var total = price * quantity;

Change this line to the following 将此行更改为以下内容

var price = 
document.querySelector(".price .woocommerce-Price-amount.amount").innerText.split(",").join("");

 // Calculation script START $(document).ready(function() { CalculateTotalPrice(); }); document.getElementsByClassName("input-text qty text")[0].onkeyup = function() { CalculateTotalPrice() }; function CalculateTotalPrice() { setTimeout(function() { var price = document.querySelector(".price .woocommerce-Price-amount.amount").innerText.split(",").join(""); var quantity = document.getElementsByClassName("input-text qty text")[0].value; var total = price * quantity; var totalOnly2Decimal = total.toFixed(2); document.getElementById("result").innerHTML = "DKK " + totalOnly2Decimal + " inkl. moms"; }, 100); } // Calculation script END 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!-- Price --> <div class="elementor-widget-container"> <p class="price">Fra: <span class="woocommerce-Price-amount amount"> <span class="woocommerce-Price-currencySymbol"></span>1,122.50 </span> kr. inkl. moms</p> </div> <!-- Quantity field --> <div class="quantity"> <label class="screen-reader-text" for="quantity_5cd3fab7bb0d7"></label> <input type="number" id="quantity_5cd3fab7bb0d7" class="input-text qty text" step="1" min="1" max="" name="quantity" value="1" title="Stk." size="4" inputmode=""> <!-- Result --> <h3 style="font-size: 17px; font-weight:bold; display:inline; text-transform:uppercase;">Total:</h3> <p class="result" id="result" style="display:inline;"></p> <script> </script> 

There seemed to be a couple of errors with your snippet that i've done my best to fixup below. 您的代码段似乎存在一些错误,我已尽力修复以下问题。 To answer your question, there are two main points: 要回答您的问题,有两个要点:

  1. var priceAsFloat = parseFloat(price.replace(/,/g, ''));

Remove the comma from the price and then convert it to a float before doing any maths with it 从价格中删除逗号,然后将其转换为浮点数,然后再进行数学运算

  1. var result = totalOnly2Decimal.toString().replace(/(\\d)(?=(\\d{3})+(?!\\d))/g, '$1,')

Convert the result back to a string and use the above regex to put commas back in at the desired points. 将结果转换回字符串,并使用上面的正则表达式将逗号放回所需的位置。

Working example: 工作示例:

 // Calculation script START $(document).ready(function() { CalculateTotalPrice(); }); document.getElementsByClassName("input-text qty text")[0].onkeyup = function() {CalculateTotalPrice()}; $("input").change(function() { CalculateTotalPrice(); }); function CalculateTotalPrice() { setTimeout(function(){ var price = document.querySelector(".price .woocommerce-Price-amount.amount").innerText; var priceWithoutCommas = price.replace(/,/g, ''); var quantity = document.getElementsByClassName("input-text qty text")[0].value; var total = priceWithoutCommas * quantity; var totalOnly2Decimal = total.toFixed(2); var result = totalOnly2Decimal.toString().replace(/(\\d)(?=(\\d{3})+(?!\\d))/g, '$1,') document.getElementById("result").innerHTML = "DKK " + result + " inkl. moms"; }, 100); } // Calculation script END 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!-- Price --> <div class="elementor-widget-container"> <p class="price">Fra: <span class="woocommerce-Price-amount amount"> <span class="woocommerce-Price-currencySymbol"></span>1,122.50 </span> kr. inkl. moms</p> </div> <!-- Quantity field --> <div class="quantity"> <label class="screen-reader-text" for="quantity_5cd3fab7bb0d7"></label> <input type="number" id="quantity_5cd3fab7bb0d7" class="input-text qty text" step="1" min="1" max="" name="quantity" value="1" title="Stk." size="4" inputmode=""> <!-- Result --> <h3 style="font-size: 17px; font-weight:bold; display:inline; text-transform:uppercase;">Total:</h3> <p class="result" id="result" style="display:inline;"></p> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM