javascript – 将公斤转换为磅和盎司的功能
作者:互联网
我想将千克转换成磅和盎司,例如如果用户输入10公斤,则该功能应返回22磅和0.73盎司
有任何想法吗?
解决方法:
根据@ dlamblin的回答,这是一个返回结构中磅数和盎司的函数.
function kToLbs(pK) {
var nearExact = pK/0.45359237;
var lbs = Math.floor(nearExact);
var oz = (nearExact - lbs) * 16;
return {
pounds: lbs,
ounces: oz
};
}
var imperial = kToLbs(10);
alert("10 kg = " + imperial.pounds + " lbs and " + imperial.ounces + " oz.");
以下是您将采用其他方式的方法:
function lbsAndOzToK(imperial) {
var pounds = imperial.pounds + imperial.ounces / 16;
return pounds * 0.45359237;
}
var kg = lbsToK({ pounds: 10, ounces: 8 });
alert("10 lbs and 8 oz = " + kg + " kg.");
标签:units-of-measurement,javascript 来源: https://codeday.me/bug/20190726/1548052.html