基于用户首选的度量单位设置,抽象出客户端度量单位转换的最佳/最优雅方法是什么?
例如,假设用户A的首选度量单位是"度量",而用户B的首选项是"英制".
现在让我说我已经计算了以平方米为单位的面积.当我去显示值时,我需要为每个用户使用不同的转换因子(例如,"1米= 1.09361码").或者说我已经计算了流体体积mL.用户B的视图将使用转换"236.588237 mL = 1 US cup"计算.
是否有一个现有的javascript库,这里有人知道处理这些琐碎的UOM转换?
jve*_*ema 40
这是一个小小的脚本,我把它扔在一起只是为了它.它处理克,字节,米和升的所有SI转换,并且我还添加了盎司和磅作为非SI单位的示例.要添加更多内容,您需要:
用法:
$u(1, 'g').as('kg').val(); // converts one gram to kg
Run Code Online (Sandbox Code Playgroud)
您可以使用.val()获取值,使用.toString()获取字符串表示,或通过.debug()获取完整详细信息
(function () {
var table = {};
window.unitConverter = function (value, unit) {
this.value = value;
if (unit) {
this.currentUnit = unit;
}
};
unitConverter.prototype.as = function (targetUnit) {
this.targetUnit = targetUnit;
return this;
};
unitConverter.prototype.is = function (currentUnit) {
this.currentUnit = currentUnit;
return this;
};
unitConverter.prototype.val = function () {
// first, convert from the current value to the base unit
var target = table[this.targetUnit];
var current = table[this.currentUnit];
if (target.base != current.base) {
throw new Error('Incompatible units; cannot convert from "' + this.currentUnit + '" to "' + this.targetUnit + '"');
}
return this.value * (current.multiplier / target.multiplier);
};
unitConverter.prototype.toString = function () {
return this.val() + ' ' + this.targetUnit;
};
unitConverter.prototype.debug = function () {
return this.value + ' ' + this.currentUnit + ' is ' + this.val() + ' ' + this.targetUnit;
};
unitConverter.addUnit = function (baseUnit, actualUnit, multiplier) {
table[actualUnit] = { base: baseUnit, actual: actualUnit, multiplier: multiplier };
};
var prefixes = ['Y', 'Z', 'E', 'P', 'T', 'G', 'M', 'k', 'h', 'da', '', 'd', 'c', 'm', 'u', 'n', 'p', 'f', 'a', 'z', 'y'];
var factors = [24, 21, 18, 15, 12, 9, 6, 3, 2, 1, 0, -1, -2, -3, -6, -9, -12, -15, -18, -21, -24];
// SI units only, that follow the mg/kg/dg/cg type of format
var units = ['g', 'b', 'l', 'm'];
for (var j = 0; j < units.length; j++) {
var base = units[j];
for (var i = 0; i < prefixes.length; i++) {
unitConverter.addUnit(base, prefixes[i] + base, Math.pow(10, factors[i]));
}
}
// we use the SI gram unit as the base; this allows
// us to convert between SI and English units
unitConverter.addUnit('g', 'ounce', 28.3495231);
unitConverter.addUnit('g', 'oz', 28.3495231);
unitConverter.addUnit('g', 'pound', 453.59237);
unitConverter.addUnit('g', 'lb', 453.59237);
window.$u = function (value, unit) {
var u = new window.unitConverter(value, unit);
return u;
};
})();
console.log($u(1, 'g').as('kg').debug());
console.log($u(1, 'kg').as('g').debug());
console.log($u(1, 'g').as('mg').debug());
console.log($u(1, 'mg').as('g').debug());
console.log($u(1, 'mg').as('kg').debug());
console.log($u(1, 'g').as('oz').debug());
console.log($u(1, 'g').as('lb').debug());
console.log($u(1, 'oz').as('lb').debug());
console.log($u(1, 'lb').as('g').debug());
// this last one throws an exception since you can't convert liters to mg
console.log($u(1, 'l').as('mg').debug());
Run Code Online (Sandbox Code Playgroud)
我把它移到了Github上的小型回购中,所以如果有人想改进/增强它们可以这样做:https: //github.com/jerodvenemafm/jsunitconverter