gpt4 book ai didi

javascript - 如何用逗号计算金额

转载 作者:行者123 更新时间:2023-12-01 01:00:15 27 4
gpt4 key购买 nike

我有一个价格为 1,500.85 的产品,我想将其与使用 javascript 输入的数量相乘。但是因为金额超过一千,所以有一个逗号,并且由于该逗号,结果显示为“NaN”..我如何用逗号计算金额?

注意:如果金额低于一千,则有效。

// 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>

最佳答案

使用 String.prototype.replace 替换所有逗号然后乘以。

let price = '1,500.85';
let quantity = '7';

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

关于javascript - 如何用逗号计算金额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56148516/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com