Skip to content
Advertisement

decimal comma remove and add comma to big number in javascript

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

<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>

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

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.`;
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement