Thursday 24 May 2018

javascript - Round converted value from html form to nearest hundredth using jQuery

I have an html form that contains an input field for a person's weight. The input field type is a textbox and allows a user to enter their weight. There is a dropdown next to it that allows for the user to convert their weight from pounds to kilograms. When the user changes the weight unit, the value in the textbox is converted.



So when a user enters their weight in pounds and then selects kilograms from the dropdown, I want this value to be rounded to the nearest hundredth.



HTML:




         




JavaScript:




var kg = 0.45359237

$(document).ready(function() {
$("#weight_unit").change(function() {
if ($(this).val() == "kg") {
($("#weight_value").val( $("#weight_value").val()*kg))
} else {
$("#weight_value").val( $("#weight_value").val()/kg)
}
});

});


Any help would be greatly appreciated!

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...