I want to remove decimal comma in result value and print numbers with commas as thousands separators.
this is the code i’m using;
my HTML
JavaScript
x
13
13
1
<form>
2
<div>
3
<h3>do</h3>
4
<input type="text" id="value1">times
5
<h4>done</h4>
6
<input type="text" id="value2">times
7
<br>
8
</div>
9
<br>
10
<button type="button" onclick="cal()"> calculate </button>
11
<h6 id="result"></h6>
12
</form>
13
JS
JavaScript
1
9
1
<script>
2
const cal = () => {
3
const A = value1.value,
4
B = value2.value;
5
const res = A * (5/100) * (100 / B);
6
result.innerText = `your have things around ${res.toString().replace(".", ",")}times.`;
7
}
8
</script>
9
thanks.
Advertisement
Answer
You could use the following method : Intl.NumberFormat https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat
I made a code pen for ya https://codepen.io/aaron-werweiss/pen/dyMeygM?editors=1010
JavaScript
1
8
1
const cal = () => {
2
const A = value1.value,
3
B = value2.value;
4
const res = A * (5/100) * (100 / B).toLocaleString();
5
6
result.innerText = `your have things around ${(new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(res))} times.`;
7
}
8