函数 toBest #
将一个单位转换为最合适的显示单位。当未提供首选单位时,函数会自动查找最佳前缀。当提供首选单位时,它会转换为值最接近 1 的单位。
Syntax #
math.toBest(unit)
math.toBest(unit, unitList)
math.toBest(unit, unitList, options)
Where #
unitList是可选的、首选目标单位的数组,可以是字符串或 Unit 类型。options是可选的对象,包含如下选项:-
offset: number大数 (BigNumber)
Parameters #
| Parameter | Type | Description |
|---|---|---|
x |
单位 | 要转换的单位 |
unitList |
Array<string> | ] 首选目标单位的可选数组。默认值:[。 |
options |
Object | 可选的 options 对象 |
Returns #
| Type | Description |
|---|---|
| 单位 | 转换为最佳匹配单位后的值 |
Throws #
Type | Description —- | ———–
Examples #
math.unit(0.05, 'm').toBest(['cm', 'mm']) // returns Unit 5 cm
math.unit(2 / 3, 'cm').toBest() // returns Unit 0.6666666666666666 cm
math.unit(10, 'm').toBest(['mm', 'km'], { offset: 1.5 }) // returns Unit 10000 mm