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
1 Answer
Reset to default 3Your 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
版权声明:本文标题:html - Convert centimeters to inches using Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744236041a2596558.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论