数字转换
Number()
对字符数字进行转换(不包括非数字)
console.log(Number('9999px')); //NaN
console.log(Number('9999')); //9999
‘+’ 转换,同Number()原理
console.log(+'9999'); //9999
parse解析
parseInt(), 整型数字
console.log(Number.parseInt('996m6')); //996
parseFloat,浮点型数字(匹配第一个 . )
console.log(Number.parseFloat('99.6.99m6')); //99.6
java不允许转换时出现非数字
System.out.println(Integer.parseInt("99px"));
//Exception in thread "main" java.lang.NumberFormatException: For input string: "99px"
isNaN
判断被传入的参数值是否不是一个数字
Number.isNaN,严格判断值是否为NaN(相当于内部调用parse)
isNaN,简易判断(相当于内部调用了Number)
console.log(+'100px'); //NaN
console.log(Number.isNaN(NaN)); //true
console.log(isNaN(NaN)); //true
console.log(Number.isNaN(100 / 0)); //false
console.log(Number.isNaN(100)); //false
console.log(isNaN('100px')); //true
console.log(Number.isNaN('100.9px')); //false
console.log(Number.isNaN(+'100px')); //true
isFinite
判断被传入的参数值是否为一个有限数值
console.log(isFinite(999)); //true
console.log(isFinite(+'999')); //true
console.log(Number.isFinite(+'999.9')); //true
console.log(isFinite('999px')); //false
console.log(isFinite(+'999px')); //false
console.log(isFinite(100 / 0)); //false
随机数
确定界限
const random = (min, max) => Math.trunc(Math.random() * (max - min) + 1) + min;
console.log(random(20, 30));
小数处理
都会进行强制类型转换
round(),四舍五入
console.log(Math.round(99.9)); //100
ceil(),向上近位
console.log(Math.ceil(99.3)); //100
floor(),向下近位
console.log(Math.floor(99.6)); //99
toFixed(),小数点保留位数,返回字符串
toFixed(0),自动四舍五入
最大安全数
console.log(Number.MAX_SAFE_INTEGER);
console.log(2 ** 53 - 1); //9007199254740991
超出的数会失去精度,造成错误计算
console.log(2 ** 53 + 5); //9007199254740996
console.log(2 ** 53 + 10); //9007199254741002
console.log(2 ** 53 + 1); //9007199254740992
BigInt()
console.log(BigInt(2 ** 53 + 5)); //9007199254740996n
console.log(999999999999999999999999999999n);
console.log(16n / 5n); //返回最接近的值 3n
console.log(16 / 5); //3.2
分数转换
import { Fraction } from 'fractional';
console.log(new Fraction(2.5).toString()); //输出 2 1/2
时间
获取当前时间戳
const time = new Date();
console.log(time.getTime());
console.log(Date.now());
LINK : Date in MDN
时间填充
padStart (targetLength, padString) , 用另一个字符串填充当前字符串
targetLength : 需要填充到的目标长度
padString : 填充字符串
const month = `${time.getMonth() + 1}`.padStart(2, 0);
console.log('Month : ' + month); //05
时间差
const time = new Date();
const time2 = new Date(2020, 1, 1);
const calDay = (time - time2) / (1000 * 60 * 60 * 24);
console.log(calDay);
Today、Yesterday
const time = new Date();
const timeArr = ['2023, 5, 12', '2023, 5, 11', '2023, 5, 5'];
for (const [i, t] of timeArr.entries()) {
const date = new Date(t);
const calDay = Math.trunc((time - date) / (1000 * 60 * 60 * 24));
calDay === 0
? console.log('Today')
: calDay === 1
? console.log('Yesterday')
: console.log(`${calDay} days ago`);
}
国际时间格式
Intl.DateTimeFormat (locale, options)
locale : 时区
options : 显示格式
英式英语 (British English) 使用 day-month-year
const intlTime = new Intl.DateTimeFormat('en-GB').format(time); // 12-5-2023
美式英语 (US English) 使用 month-day-year
const intlTime = new Intl.DateTimeFormat('en-US').format(time); // 5/12/2023
标准格式
const time = new Date();
const options = {
hour: 'numeric',
minute: 'numeric',
day: 'numeric',
month: 'long',
};
const locale = navigator.language;
console.log(locale);
const intlTime = new Intl.DateTimeFormat(locale, options).format(time);
console.log(intlTime);
延时器
setTimeout (functionRef, delay, param)
…解构运算符
const materials = ['eggs', 'butter', 'fruits'];
console.log(...materials); //eggs butter fruits
const materials = ['eggs', 'butter', 'fruits'];
const cakeTimer = setTimeout(
(p1, p2, p3) =>
console.log(`${p1} and ${p2} and ${p3} is the materials of making CAKE`),
2000,
...materials
);
console.log(cakeTimer);
if (materials.includes('eggs')) {
clearTimeout(cakeTimer);
}
定时器
setInterval (func, [delay, arg1, arg2, …]) ,需要手动关闭
<h1>00 : 00</h1>
const timer = document.querySelector("h1");
let time = 3;
const interval = setInterval(() => {
minute = String(Math.trunc(time / 60)).padStart(2, 0);
sec = String(Math.trunc(time % 60)).padStart(2, 0);
timer.textContent = `${minute} : ${sec}`;
time--;
if (time < 0) clearInterval(interval);
}, 1000);