0

I have a working code which define two const with js and then multiply those two const to get third value. But what I need is to add zeroes in the end of the values and I need help with that:

<p> Total odd:  <span id = "totaleur_id"> </span> </p>
<p> Stake:      <span id = "steur_id">    </span> </p>
<p> Return:     <span id = "posseur_id">  </span> </p>


<script>
const totaleur = 1.80
const steur = 50
const possibleWinnings = totaleur * steur
document.querySelector("#totaleur_id").textContent = totaleur;
document.querySelector("#steur_id").textContent = steur;
document.querySelector("#posseur_id").textContent = possibleWinnings;
</script>

What I get as an frontend result is:

Total odd: 1.8
Stake:    50
Return:   90

But I need the result to be:

Total odd: 1.80
Stake:    50
Return:   90.00

And other frontend result example:

Total odd: 2.35
Stake:     50
Return:    117.5 //this should be: 117.50
Fantantonio
  • 377
  • 4
  • 15
  • Hi! You can use the `toFixed` method: `totaleur.toFixed(2)` – Fel Nov 05 '20 at 09:24
  • Does this answer your question? [Format number to always show 2 decimal places](https://stackoverflow.com/questions/6134039/format-number-to-always-show-2-decimal-places) – Liam Nov 25 '20 at 08:46

1 Answers1

4

Use the toFixed method passing as input the number of numbers you want after the comma:

const possibleWinnings = (totaleur * steur).toFixed(2)
Fantantonio
  • 377
  • 4
  • 15