我不是很擅长这一点,并且已经尝试了几天上下搜索任何答案以帮助解决我的问题。下面我只拼凑了一起。我想获取生成的span类值,/ 12,向上舍入到最近的便士,并显示在同一网页上的其他位置。任何帮助将不胜感激,并提前感谢您。
const price = document.getElementsByClassName('price-value')[0].innerHTML; // Price of item before tax
// Calculate total after tax to two decimal places
let totalPrice = price / 12;
totalPrice.toFixed(2);
document.getElementById("demo").innerHTML =totalPrice

<span class="price-value model-price-value-sale">
$1,349.95
</span>
<p id="demo"></p>
&#13;
答案 0 :(得分:0)
首先,您需要通过以下方式将货币价格从货币转换为实际数字:
Number(price.replace(/[^0-9\.-]+/g,""))
然后使用Math.ceil()
函数进行舍入。
所以它会是这样的:
const price = document.getElementsByClassName('price-value')[0].innerHTML; // Price of item before tax
// Calculate total after tax to two decimal places
//let totalPrice = Math.ceil(Number(price.replace(/[^0-9\.-]+/g,"")) / 12);
let totalPrice = Math.round((Number(price.replace(/[^0-9\.-]+/g,"")) / 12)* 100)/100;
document.getElementById("demo").innerHTML =totalPrice
&#13;
<span class="price-value model-price-value-sale">
$1,349.95
</span>
<p id="demo"></p>
&#13;