Skip to content

number api

floor

向后

js
Math.floor(2.9); // 2
Math.floor(2.1); // 2

Math.floor(-1.9); // -2
Math.floor(-1.1); // -2

ceil

向前

js
Math.ceil(2.9); // 3
Math.ceil(2.1); // 3

Math.ceil(-1.9); // -1
Math.ceil(-1.1); // -1

round

四舍五入

js
Math.round(2.9); // 3
Math.round(2.1); // 2

Math.round(-1.9); // -2
Math.round(-1.1); // -1

trunc

移除 小数点后数字

js
Math.trunc(2.9); // 2
Math.trunc(2.1); // 2

Math.trunc(-1.9); // -1
Math.trunc(-1.1); // -1

toFixed

toFixed 的结果是 string

js
(12).toFixed(2); // '12.00'

isNaN 和 Number.isNaN

isFinite 和 Number.isFinite

Object.is

  • NaN
  • 0 和 -0

parseInt 和 parseFloat

js
parseInt("bhl41i"); // NaN
parseInt("bhl41i", 36); // 694666422

parseInt("  100 0 "); // 100

parseInt("100c"); // 100
parseInt("c100"); // NaN!!

parseInt("192.168.0.1"); // 192
parseFloat("192.168.0.1"); // 192.168

Math

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Math

random / 随机数
返回一个从 0 到 1 的随机数(不包括 1)

max min / 最大最小
参数没有限制

pow / 幂
Math.pow(n, power)