Search code examples
javascripthtmlaudioinnerhtml

How can I avoid decimals in a number value that is shown with innerHTML


The code is for an audio system when the user presses Up arrow or Down arrow then the volume changing with 0.05 up or down (5%) on the audio volume. That code isn't important. I just said because the changing value is 0.05 that means when I want to display the current volume the numbers sometimes look like this:
A good one:
A bad one:

Anything I could use? Maybe RegEx? (but how) Here is the code:

Element.innerHTML = "Hangerő: <strong>" + (audio_element.volume * 100) + "%</strong>"

Solution

  • Try parseInt() .

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

    Element.innerHTML = "Hangerő: <strong>" + parseInt(audio_element.volume * 100) + "%</strong>"