在跨度 Class 中捕获货币,除以 12 向上舍入并显示
Capture Currency in Span Class, Divide by 12 Round up and Display
我不太擅长这个,最近几天我尝试上下搜索任何类型的答案来帮助解决我的问题。我只拼凑了以下内容。我想获取生成的跨度 class 值,/乘以 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">
,349.95
</span>
<p id="demo"></p>
首先,您需要通过以下方式将价格从货币转换为实际数字:
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
<span class="price-value model-price-value-sale">
,349.95
</span>
<p id="demo"></p>
我不太擅长这个,最近几天我尝试上下搜索任何类型的答案来帮助解决我的问题。我只拼凑了以下内容。我想获取生成的跨度 class 值,/乘以 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">
,349.95
</span>
<p id="demo"></p>
首先,您需要通过以下方式将价格从货币转换为实际数字:
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
<span class="price-value model-price-value-sale">
,349.95
</span>
<p id="demo"></p>