2025-07-25 10:36:51 +02:00

1.9 KiB

layout
default

Function toBest #

Converts a unit to the most appropriate display unit. When no preferred units are provided, the function automatically find the best prefix. When preferred units are provided, it converts to the unit that gives a value closest to 1.

Syntax #

math.toBest(unit)
math.toBest(unit, unitList)
math.toBest(unit, unitList, options)

Where #

  • unitList is an optional array of preferred target units as string or Unit.
  • options is an optional object with options, formed as follows:
  • offset: number | BigNumber

Parameters #

Parameter Type Description
x Unit The unit to be converted
unitList Array<string> ] Optional array of preferred target units Default value: [.
options Object Optional options object

Returns #

Type Description
Unit Value converted to the best matching unit

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

See also #

unit, to, format