小数逗号删除并在 javascript 中的大数字中添加逗号
decimal comma remove and add comma to big number in javascript
我想删除结果值中的小数点逗号并打印以逗号作为千位分隔符的数字。
这是我正在使用的代码;
我的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>
谢谢。
您可以使用以下方法: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.`;
}
我想删除结果值中的小数点逗号并打印以逗号作为千位分隔符的数字。
这是我正在使用的代码;
我的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>
谢谢。
您可以使用以下方法: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.`;
}