1

My employer wishes for a textbox to convert feet to metres to one decimal place, however, I am unfamiliar with Javascript and only know the bare basics for HTML purposes. My current code converts it correctly, but it is not to one decimal place.. Any suggestions?

Code:

    <script type="text/javascript">
<!--
function validate() {
var ft = document.getElementById('LengthFt');
var res=0.3048*ft.value;
var mtrs = document.getElementById('LengthMtrs');
mtrs.value = res;
}
//-->
</script>
PwnageAtPwn
  • 431
  • 1
  • 6
  • 21

1 Answers1

1

It looks like you've done the conversion. After that, all you need to do is format the number properly.

The toFixed method can be used to format to a fixed number of decimal places.

Replacing

mtrs.value = res;

with

mtrs.value = res.toFixed(1);

should leave the right number in the LengthMtrs input accurate to the closest decimeter.

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245