简体   繁体   English

javascript 货币乘以数字

[英]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.它运作良好,但问题是 ID 价格的内容,因为我需要像这样的 output 2,920,000。(它的价格 * 数量),但采用印尼盾货币格式。 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 ","?因为如果我把 1,460,000 没有“,”我的代码工作得很好,但是我怎么能用“,”来计算,而 output 用“,”呢?

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.这个 function 将删除逗号并将其转换为可以格式化的浮点数。

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>

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

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