本文整理汇总了TypeScript中js-quantities.to函数的典型用法代码示例。如果您正苦于以下问题:TypeScript to函数的具体用法?TypeScript to怎么用?TypeScript to使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了to函数的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: it
it("should calculate inverses", () => {
var qty = Qty("1 ohm");
var result = qty.to("siemens");
expect(result.scalar).toBe(1);
expect(result.kind()).toBe("conductance");
qty = Qty("10 ohm");
result = qty.to("siemens");
expect(result.scalar).toBe(0.1);
expect(result.kind()).toBe("conductance");
qty = Qty("10 siemens");
result = qty.to("ohm");
expect(result.scalar).toBe(0.1);
expect(result.kind()).toBe("resistance");
qty = Qty("10 siemens");
result = qty.inverse();
expect(result.eq(".1 ohm")).toBe(true);
expect(result.kind()).toBe("resistance");
// cannot inverse a quantity with a 0 scalar
qty = Qty("0 ohm");
expect(() => { qty.inverse(); }).toThrow("Divide by zero");
qty = Qty("10 ohm").inverse();
result = qty.to("S");
expect(result.scalar).toBe(0.1);
expect(result.kind()).toBe("conductance");
qty = Qty("12 in").inverse();
// TODO: Swap toBeCloseTo with toBe once divSafe is fixed
expect(qty.to("ft").scalar).toBeCloseTo(1, 10);
});
示例2: Qty
const qty1 = Qty('1m');
const qty2 = Qty('2m');
qty1.isCompatible(qty2); // => true or false
qty.kind(); // => 'length', 'area', etc...
qty.isUnitless(); // => true or false
qty.isBase(); // => true if quantity is represented with base units
qty.toBase(); // converts to SI units (10 cm => 0.1 m) (new instance)
qty.toFloat(); // returns scalar of unitless quantity (otherwise throws error)
qty.to('m'); // converts quantity to meter if compatible or throws an error (new instance)
qty1.to(qty2); // converts quantity to same unit of qty2 if compatible or throws an error (new instance)
qty.inverse(); // converts quantity to its inverse ('100 m/s' => '.01 s/m')
// Inverses can be used, but there is no special checking to
// rename the units
Qty('10ohm').inverse(); // '.1/ohm' (not '.1S', although they are equivalent)
// however, the 'to' command will convert between inverses also
Qty('10ohm').to('S'); // '.1S'
var convert = Qty.swiftConverter('m/h', 'ft/s'); // Configures converter
// Converting single value
var converted = convert(2500); // => 2.278..
// Converting large array of values