Skip to content

Latest commit

 

History

History
272 lines (175 loc) · 4.98 KB

index.md

File metadata and controls

272 lines (175 loc) · 4.98 KB

MATH 数学

方法

$core.add 加法

$core.sub 减法

$core.mul 乘法

$core.div 除法

$core.min 获取最小数

$core.max 获取最大数

$core.sum 求和

$core.mean 平均数

$core.floor 按指定精度向下获取

$core.ceil 按指定精度向上获取

$core.round 按指定精度四舍五入获取

$core.add 加法

$core.add两数相加,返回两数的和。

语法

$core.add(augend, addend)

参数

  • augend 非必填,加数
  • addend 非必填,被加数

返回值

返回两数的和,和可以使数值Number类型或者大整数BigInt类型。

例子

$core.add(1, 2); // 3
$core.add(1, 2n); // 3 不推荐
$core.add(1n, 2n); // 3n
$core.add(0.1, 0.2); // 0.3

减法

$core.sub两数相减,返回两数的差。

语法

$core.sub(minuend, subtrahend)

参数

  • minuend 非必填,被减数
  • subtrahend 非必填,减数

返回值

返回两数的差,差可以使数值Number类型或者大整数BigInt类型。

例子

$core.sub(3, 2); // 1
$core.sub(3, 2n); // 1
$core.sub(3n, 2n); // 1n
$core.sub(0.3, 0.2); // 0.1

乘法

$core.mul两数相乘,返回两数的积。

语法

$core.mul(multiplier, multiplicand)

参数

  • multiplier 非必填,乘数
  • multiplicand 非必填,被乘数

返回值

返回两数的积,积可以使数值Number类型或者大整数BigInt类型。

例子

$core.mul(1, 2); // 2
$core.mul(1, 2n); // 2
$core.mul(1n, 2n); // 2n
$core.mul(0.1, 0.2); // 0.02

除法

$core.div两数相除,返回两数的商。

语法

$core.div(dividend, divisor)

参数

  • dividend 非必填,被除数
  • divisor 非必填,除数

返回值

返回两数的商,商可以使数值Number类型或者大整数BigInt类型。

例子

$core.div(1, 2); // 0.5
$core.div(1, 2n); // 0.5
$core.div(1n, 2n); // 0n
$core.div(0.1, 0.2); // 0.5

最小值

$core.min方法找到数组中最小元素的项,如果是空数组返回 undefine。

语法

$core.min(array, iteratee)

参数

  • array 数组,默认获取数值中最小的元素。
  • iteratee 非必填,数组中的元素按照 iteratee 函数规则获取最小的元素。

返回值

返回数组中最小的元素,如果是空数组返回 undefined。

例子

例一、

$core.min([3, 1, 9, 5, 7]); // 1

例二、

$core.min([
  { name: "a", age: 3 },
  { name: "b", age: 1 },
  { name: "c", age: 9 },
  { name: "d", age: 5 },
  { name: "e", age: 7 },
]); // { name: 'b', age: 1 }

最大值

$core.max 方法指找到数组中最大元素的项,如果是空数组返回 undefine。

语法

$core.max(array, iteratee)

参数

  • array 数组,默认获取数值中最小的元素。
  • iteratee 非必填,数组中的元素按照 iteratee 函数规则获取最小的元素。

返回值

返回数组中最大的元素,如果是空数组返回 undefined。

例子

例一、

$core.max([3, 1, 9, 5, 7]); // 9

例二、

$core.max([
  { name: "a", age: 3 },
  { name: "b", age: 1 },
  { name: "c", age: 9 },
  { name: "d", age: 5 },
  { name: "e", age: 7 },
]); // { name: 'c', age: 9 }

求和

语法

$core.sum(array, iteratee)

参数

  • array 数组。
  • iteratee 非必填,数组中的元素按照 iteratee 函数规则的返回值进行累加求和。

返回值

返回数组中元素的和。

例子

例一、

$core.sum([3, 1, 9, 5, 7]); // 25

例二、

$core.sum(
  [
    { name: "a", age: 3 },
    { name: "b", age: 1 },
    { name: "c", age: 9 },
    { name: "d", age: 5 },
    { name: "e", age: 7 },
  ],
  function (item) {
    return item.age * 10;
  }
); // 25

平均值

$core.mean([3, 1, 9, 5, 7]); // 5
$core.mean(
  [
    { name: "a", age: 3 },
    { name: "b", age: 1 },
    { name: "c", age: 9 },
    { name: "d", age: 5 },
    { name: "e", age: 7 },
  ],
  function (item) {
    return item.age * 10;
  }
); // 5