Pregunta

 $('#pm').val(Math.floor(parseFloat(pm*100/100)));

Full code:

<script type="text/javascript">     
        function updatePay() {
            // Grab all the value just incase they're needed.
            var current_price = <?php echo json_encode($current_price); ?>;
            var pm = $('#pm').val();
            var gg = pm/current_price;

            // Set the new input values.
           $('#pm').val(Math.floor(parseFloat(pm*100/100)));
            $('#gg').val(gg);
        }

        $('#pm').keyup(updatePay);
        $('#gg').keyup(updatePay);

    </script>

When I use Math.floor it doesn't allow me to enter a second decimal.

I need my code to be able to allow a second decimal place to be filled in, how can I do this in Javascript?

¿Fue útil?

Solución

Try this

$('#pm').val((Math.floor(parseFloat(pm)*100)/100).toFixed(2));

I think you want to round down and allow 2 decimal places,

so if the number is 3546.699433
parseFloat(pm)*100 = 354669.9433

math.floor(354669.9433) = 354669

354669/100 = 3546.69

<script type="text/javascript">     
        function updatePay() {
            // Grab all the value just incase they're needed.
            var current_price = <?php echo json_encode($current_price); ?>;
            var pm = $('#pm').val();
            var gg = pm/current_price;

            // Set the new input values.
            $('#pm').val((Math.floor(parseFloat(pm)*100)/100).toFixed(2));
            $('#gg').val(gg);
        }

        $('#pm').change(updatePay);
        $('#gg').chnage(updatePay);

    </script>

If you want something that gets updated on keyup, try something along these lines

Javascript:

   document.getElementById("num1").onkeyup = function(){
        var val = (Math.floor(parseFloat( document.getElementById("num1").value)*100)/100).toFixed(2);
        if(isNaN(val)){
            document.getElementById("result").innerHTML = "pm will appear here";
        }
        else if(val){
            document.getElementById("result").innerHTML = val;
        } else {
            document.getElementById("result").innerHTML = "pm will appear here";
        }


    }

HTML:

<body>
    <input type="button" id="myButton" value="click me"/>
    <span id="result"></span>
    <input type="text" id="num1" value="1.1111"></div>

</body>

Otros consejos

It's hard to tell what you're trying to achieve, but at a guess I suspect you want the values displayed in pm and gg to have two digits of fractional precision. If so:

function updatePay() {
    // Grab all the value just incase they're needed.
    var current_price = <?php echo json_encode($current_price); ?>;
    var pm = parseFloat($('#pm').val()); // Parse here
    var gg = pm/current_price;

    // Set the new input values.
    $('#pm').val(pm.toFixed(2));         // Round to two places and turn back into text
    $('#gg').val(gg.toFixed(2));         // " " " " " " " " "
}

Side note: You have this set as a keyup handler on the gg element, but you're always overwriting what the gg element has in it, without using the value in gg at all. If I were a user, I'd find that pretty irritating.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top