将公斤转换为磅和盎司的功能

问题描述 投票:0回答:5

我想将公斤转换为磅和盎司,例如如果用户输入 10 千克,则该函数应返回 22 磅和 0.73 盎司

有什么想法吗?

javascript data-conversion units-of-measurement
5个回答
7
投票

根据@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.");

2
投票
function kgToPounds(value) {
    return value * ?conversionValue?;
}

?conversionValue?
替换为所需的速率。

function poundsToOunces(value) {
    return value * 16;
}

这并不是什么难事。


1
投票
function KtoLbs(pK) {
  nearExact = pK/0.45359237;
  lbs = Math.floor(nearExact);
  oz = (nearExact - lbs) * 16;
}
/* sigh */

1
投票

谷歌几乎做到了。不会做磅和盎司。

Google“10 公斤(盎司)”

Google 回应:10 公斤 = 352.739619 盎司

然后您所要做的就是编写所有管道以将信息发送到谷歌并将其返回。

:-)


0
投票

这是一个简单的转换...我会将详细信息留给您...

1 公斤 = 2.20462262 磅

© www.soinside.com 2019 - 2024. All rights reserved.