Javascript/jQuery - 以英尺和英寸为单位显示英寸
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13723585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript/jQuery - Display inches in feet and inches
提问by AT92
I'm making a BMI calculator (see JSFiddle - http://jsfiddle.net/b5ww2/) and I want the height in inches to display in feet and inches. I also want the weight in pounds to be displayed in stones and pounds.
我正在制作一个 BMI 计算器(参见 JSFiddle - http://jsfiddle.net/b5ww2/),我希望以英寸为单位的高度以英尺和英寸为单位显示。我还希望以磅为单位的重量以石头和磅为单位显示。
This is the code I'm using to convert the slider value to cm and inches:
这是我用来将滑块值转换为厘米和英寸的代码:
slide: function( event, ui ) {
$( "#heightslidecm" ).html( ui.value + 'cm' );
$( "#heightslidein" ).html( (ui.value*0.393700787).toFixed(0) + 'in' );
}
My js knowledge isn't great - especially when it comes to the math aspect.
我的 js 知识不是很好 - 特别是在数学方面。
Any ideas?
有任何想法吗?
Thanks in advance
提前致谢
回答by Levi Botelho
You simply need to include the logic for the conversion. Here is an example for feet and inches:
您只需要包含转换逻辑。以下是英尺和英寸的示例:
slide: function( event, ui ) {
$( "#heightslidecm" ).html( ui.value + 'cm' );
var inches = (ui.value*0.393700787).toFixed(0);
var feet = Math.floor(inches / 12);
inches %= 12;
$( "#heightslidein" ).html( feet + "ft " + inches + 'in');
}
You just need to repeat this same logic for the pounds/stone using the appropriate conversion values.
您只需要使用适当的转换值对磅/石重复相同的逻辑。
回答by Necevil
OK — after googling and landing here I tried the above. Even though the post is related to converting Centimeters (CM) to inches, it comes up in Google for ANY conversion (and Inches to Feet and Inches in my case).
好的 - 在谷歌搜索并登陆这里后,我尝试了上述方法。尽管该帖子与将厘米 (CM) 转换为英寸有关,但它会出现在 Google 中以进行任何转换(在我的情况下是英寸到英尺和英寸)。
For my project I was trying to convert INCHES to Feet and Inches (and yet here I am). To save anyone else who encounters the INCHES to Feet and Inches question one extra uGoogle-eh here is how I would do that in Javascript:
对于我的项目,我试图将英寸转换为英尺和英寸(但我在这里)。为了拯救遇到英寸到英尺和英寸问题的其他人,这里有一个额外的 uGoogle-eh 是我将如何在 Javascript 中做到这一点:
let height = 75; // this could be any number of inches
let feet = Math.floor(height / 12);
let inches = (height - (feet * 12));
The above should let you convert from Inches to feet and inches (if Google un-knowingly landed you here like it did for me). Good luck out there!
以上应该可以让你从英寸转换为英尺和英寸(如果谷歌像我那样在不知不觉中把你带到这里)。祝你好运!