将厘米转换为英寸 [英] Convert centimeters to inches
问题描述
我需要一个JavaScript函数将CM转换为IN。我一直在使用以下内容:
$ p $ function toFeet(n){
var realFeet =((n * 0.393700) / 12);
var feet = Math.floor(realFeet);
var inches = Math.round((realFeet - feet)* 12);
return feet +& prime; +英寸+'和'素';';
}
可以将100cm转换成3'3。 CM(澳大利亚),但从检查转换网站,这似乎是错误的。
任何建议?
你的代码是正确的,并给出正确的结果。
我会按照以下方式更改它,所以我相信我不会失去准确性使用近似的数字进行转换,这对你的情况可能没用,但有时可能会起作用(例如计算地图上的距离)。
var realFeet = n / 30.48; // = 12 * 2.54
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((realFeet - feet) * 12);
return feet + "′" + inches + '″';
}
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?
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 come into play (e.g. calculating distances on maps).
var realFeet = n / 30.48; // = 12 * 2.54
这篇关于将厘米转换为英寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!