String 다음으로 외워야 할 표준 내장 객체들이다. 필요할때 딱 떠오를 정도로 외워야 한다.
const num = 3.1415926535
console.log(num.toFixed(2)) // '3.14'
console.log(parseFloat(num.toFixed(3))) // 3.141
const num = 1000000
console.log(num.toLocaleString()) // '1,000,000'
console.log(`${num.toLocaleString()}원`) // '1,000,000원'
const num = 123
const pi = 3.14
console.log(Number.isInteger(num)) // true
console.log(Number.isInteger(pi)) // false
const num1 = NaN
const num2 = 123
const str = 'Hello world'
const nul = null
console.log(Number.isNaN(num1)) // true
console.log(Number.isNaN(num2)) // false
console.log(Number.isNaN(str)) // false
console.log(Number.isNaN(nul)) // false
const str = '3.1415926535'
// 10진수 정수로 반환!
console.log(Number.parseInt(str, 10)) // 3
console.log(Number.parseInt(num, 10)) // 3
const str = '3.1415926535'
const num = 3.1415926535
console.log(Number.parseFloat(str)) // 3.1415926535
console.log(Number.parseFloat(num)) // 3.1415926535
console.log(Math.abs(2)) // 2
console.log(Math.abs(-2)) // 2
console.log(Math.ceil(3.1415926535)) // 4
console.log(Math.floor(3.1415926535)) // 3
const num1 = 3.141
const num2 = 3.768
console.log(Math.round(num1)) // 3
console.log(Math.round(num2)) // 4
console.log(Math.max(1, 22, 38, 192)) // 192
console.log(Math.min(1, 22, 38, 192)) // 1
console.log(Math.pow(4, 2)) // 16
console.log(Math.pow(7, 2)) // 49
console.log(Math.pow(10, 3)) // 1000
console.log(Math.random()) // 0.6903810349799351
// 특정 범위의 무작위 정수를 얻는 함수
// 0~9 사이 정수
function random(min = 0, max = 10) {
return Math.floor(Math.random() * (max - min)) + min
}
console.log(random()) // 0~9
console.log(random(11, 20)) // 11~19
console.log(random(101, 1000)) // 101~999
//// 원리! ////
// const ran = Math.random()
// const max = 5
// const min = 1
//
// console.log(ran) // 무작위 수 생성!
// console.log(ran * (max - min)) // 범위의 최댓값 만들기!
// console.log(Math.floor(ran * (max - min))) // 소수점 내림!
// console.log(Math.floor(ran * (max - min)) + min) // 범위의 최솟값 만들기!