Add another zero to the result

1

I have the following code and I need that for example, if in the first field I enter 10 and in the second 20 , it shows me:

  

0.5 USD

but I need you to show

  

0.50 USD

<form oninput="x.value=parseFloat(peso.value)/parseFloat(dolar.value)">

        <input type="number" id="peso" placeholder="pesos" value="">
        <br>
        <input type="number" id="dolar" placeholder="dolares" value="">
        <hr>

        <output name="x"></output> USD

</form>
    
asked by Luis Hernandez 29.06.2018 в 01:31
source

2 answers

2

I see that you just answered BDOM correctly, but anyway I give you your example that I just tried to work as you indicated, as BDOM has told you to use the method toFixed ()

<form oninput="x.value=(parseFloat(peso.value,2)/parseFloat(dolar.value)).toFixed(2)">

        <input type="number" id="peso" placeholder="pesos" value="">
        <br>
        <input type="number" id="dolar" placeholder="dolares" value="">
        <hr>

        <output name="x"></output> USD

</form>
    
answered by 29.06.2018 / 01:48
source
3

Use Number.prototype.toFixed . With this you indicate that you want 2 decimals after the period. For example:

var a = 15.3;
alert(a.toFixed(2));

I hope I help you.

    
answered by 29.06.2018 в 01:41