Skip to content

Sys.Math

Sys.Math aggregates lists of numbers and rounds numbers to a given number of decimal digits. The …Map variants first take one number from each item of a list of objects, either by property name or with a function.

Availability: Everywhere

sum(items: number[]): number

Returns the sum of the numbers. An empty list gives 0.

ParameterTypeDescription
itemsnumber[]Numbers to add up.

Returns: number · Availability: Everywhere

sumMap<T extends object>(items: T[], propertyOrFn: NumberPropertyKeys<T> | ((item: T) => number)): number

Returns the sum of one number taken from each item.

ParameterTypeDescription
itemsT[]Objects to aggregate.
propertyOrFnNumberPropertyKeys<T> | ((item: T) => number)Name of the property to read from each item, or a function that returns the number for an item.
const total = Sys.Math.sumMap(order.positions, 'amount')
const totalWithTax = Sys.Math.sumMap(order.positions, (p) => p.amount * (1 + p.taxRate))

Returns: number · Availability: Everywhere

Note

The type check accepts any property name of T, not only properties of type number. A non-numeric property gives a meaningless result.

avg(items: number[]): number

Returns the arithmetic mean of the numbers. An empty list gives NaN.

ParameterTypeDescription
itemsnumber[]Numbers to average.

Returns: number · Availability: Everywhere

avgMap<T extends object>(items: T[], propertyOrFn: NumberPropertyKeys<T> | ((item: T) => number)): number

Returns the arithmetic mean of one number taken from each item. An empty list gives NaN.

ParameterTypeDescription
itemsT[]Objects to aggregate.
propertyOrFnNumberPropertyKeys<T> | ((item: T) => number)Name of the property to read from each item, or a function that returns the number for an item.

Returns: number · Availability: Everywhere

min(items: number[]): number

Returns the smallest of the numbers. An empty list gives Infinity.

ParameterTypeDescription
itemsnumber[]Numbers to compare.

Returns: number · Availability: Everywhere

minMap<T extends object>(items: T[], propertyOrFn: NumberPropertyKeys<T> | ((item: T) => number)): number

Returns the smallest of one number taken from each item. An empty list gives Infinity.

ParameterTypeDescription
itemsT[]Objects to aggregate.
propertyOrFnNumberPropertyKeys<T> | ((item: T) => number)Name of the property to read from each item, or a function that returns the number for an item.

Returns: number · Availability: Everywhere

max(items: number[]): number

Returns the largest of the numbers. An empty list gives -Infinity.

ParameterTypeDescription
itemsnumber[]Numbers to compare.

Returns: number · Availability: Everywhere

maxMap<T extends object>(items: T[], propertyOrFn: NumberPropertyKeys<T> | ((item: T) => number)): number

Returns the largest of one number taken from each item. An empty list gives -Infinity.

ParameterTypeDescription
itemsT[]Objects to aggregate.
propertyOrFnNumberPropertyKeys<T> | ((item: T) => number)Name of the property to read from each item, or a function that returns the number for an item.

Returns: number · Availability: Everywhere

roundToDigits(value: number, digits: number): number

Rounds a number to the given number of decimal digits, with halves rounded up (towards positive infinity), as Math.round does.

ParameterTypeDescription
valuenumberNumber to round.
digitsnumberNumber of decimal digits to keep. A negative value rounds to tens, hundreds, and so on.

Returns: number · Availability: Everywhere

roundUpToDigits(value: number, digits: number): number

Rounds a number up (towards positive infinity) to the given number of decimal digits.

ParameterTypeDescription
valuenumberNumber to round.
digitsnumberNumber of decimal digits to keep.

Returns: number · Availability: Everywhere

roundDownToDigits(value: number, digits: number): number

Rounds a number down (towards negative infinity) to the given number of decimal digits.

ParameterTypeDescription
valuenumberNumber to round.
digitsnumberNumber of decimal digits to keep.

Returns: number · Availability: Everywhere