admin管理员组

文章数量:1401431

I need a JavaScript function to convert CM to IN. I have been using the following:

function toFeet(n) {
  var realFeet = ((n*0.393700) / 12);
  var feet = Math.floor(realFeet);
  var inches = Math.round(10*((realFeet - feet) * 12)) / 10;
  return feet + "′" + inches + '″';
}

console.log(toFeet(100));

I need a JavaScript function to convert CM to IN. I have been using the following:

function toFeet(n) {
  var realFeet = ((n*0.393700) / 12);
  var feet = Math.floor(realFeet);
  var inches = Math.round(10*((realFeet - feet) * 12)) / 10;
  return feet + "′" + inches + '″';
}

console.log(toFeet(100));

The catch is it converts 100cm into 3'3". I only deal in CM (australia) but from checking on conversion sites it appears this is wrong.

Any advice?

Share Improve this question edited Nov 11, 2021 at 17:08 HoldOffHunger 21k11 gold badges120 silver badges146 bronze badges asked May 14, 2013 at 10:57 AdamAdam 21k38 gold badges132 silver badges220 bronze badges 6
  • 1 3'3" means 3 feet and 3 inches, wich is correct. – Aioros Commented May 14, 2013 at 11:00
  • this might help:: javascriptkit./script/script2/inchconvert.shtml – Sudhir Bastakoti Commented May 14, 2013 at 11:00
  • 1 100cm is 39.37 inches, which rounds to 39 inches, which is 3 feet, 3 inches, so it looks like your function works correctly. – RB. Commented May 14, 2013 at 11:01
  • 1 1" = 2.54 cm... 1' = 12". So 100 cm = 100 / 2.54 = 39.37" = ~3'3" – Sani Huttunen Commented May 14, 2013 at 11:01
  • ok ok - thankyou... I did see 39.37 inches but didn't understand it... thankyou :) – Adam Commented May 14, 2013 at 11:02
 |  Show 1 more ment

1 Answer 1

Reset to default 3

Your code is correct and gives the right result.

I would change it in the following way, so I am sure I do not lose precision using an approximate number for the conversion. This may be useless in your case, but sometimes could e into play (e.g. calculating distances on maps).

var realFeet = n / 30.48; // = 12 * 2.54

本文标签: htmlConvert centimeters to inches using JavascriptStack Overflow