【问题标题】:decimal comma remove and add comma to big number in javascript十进制逗号删除并将逗号添加到javascript中的大数字
【发布时间】:2020-09-11 06:42:59
【问题描述】:

我想删除结果值中的小数逗号,并以逗号作为千位分隔符打印数字。

这是我正在使用的代码;

我的 HTML

<form>
<div>
<h3>do</h3>
<input type="text" id="value1">times
<h4>done</h4>
<input type="text" id="value2">times
<br>
</div>
<br>
<button type="button" onclick="cal()"> calculate </button>
<h6 id="result"></h6>
</form>

JS

<script>
const cal = () => {
const A = value1.value,
      B = value2.value;
const res = A * (5/100) * (100 / B);
result.innerText = `your have things around ${res.toString().replace(".", ",")}times.`;
}
</script>

谢谢。

【问题讨论】:

  • 您可能想看看 ECMAScript 国际化 API,尤其是 NumberFormat 函数

标签: javascript decimal comma


【解决方案1】:

您可以使用以下方法:Intl.NumberFormat https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat

我为你做了一个代码笔https://codepen.io/aaron-werweiss/pen/dyMeygM?editors=1010

const cal = () => {
const A = value1.value,
      B = value2.value;
const res = A * (5/100) * (100 / B).toLocaleString();
  
result.innerText = `your have things around ${(new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(res))} times.`;
}

【讨论】:

  • 谢谢。如果你不介意你会教我如何在我的输入框中添加逗号到数字吗?
  • 您需要一个动态检查输入的脚本。此线程中使用 jquery 提供了一个解决方案:stackoverflow.com/questions/40738228/… 如果解决了您的问题,请将原始问题标记为已回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-22
  • 1970-01-01
  • 1970-01-01
  • 2010-12-07
  • 1970-01-01
  • 2012-11-20
相关资源
最近更新 更多