Wednesday 5 April 2017

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

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...