简体   繁体   中英

javascript multiply currency with number

I have following code:

 var a = document.getElementById("price").innerHTML; var b = document.getElementById("qty").innerHTML; var total = a * b document.getElementById("ttl").innerHTML = total.toString().replace(/\B(?=(\d{3})+(?,\d))/g, ";");
 <p id="price">1,460,000</p> <p id="qty">2</p> <p id="ttl"></p>

It work well,but problem is content of ID-price,because i need output like this 2,920,000.(its price * qty),but in indonesian rupiah currency format. I want to ask,how can "unformat" that price and then make calculation then format again? Because if i put 1,460,000 without "," my code work well,but how can i count with ",",and have output with ","?

you can use Number formatters

let currencyFormatterInteger = new Intl.NumberFormat(locale, {
    style: "currency",
    currency: currency,
    minimumFractionDigits: 0,
  });

currencyFormatterInteger.format(price / 100)

This function will remove the commas and convert it to float which can be formatted.

parseFloat('100,000.00'.replace(/,/g, ''));

 var a = document.getElementById("price").innerHTML; var b = document.getElementById("qty").innerHTML; //convert to number a = Number(a.replace(/[^0-9.-]+/g,"")); b = Number(b.replace(/[^0-9.-]+/g,"")); var total = a * b; //convert string again document.getElementById("ttl").innerHTML = total.toString().replace(/\B(?=(\d{3})+(?,\d))/g, ";");
 <p id="price">1,460,000</p> <p id="qty">2</p> <p id="ttl"></p>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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