Math
Math λ μνμ μΈ μμμ ν¨μλ₯Ό μν μμ±κ³Ό λ©μλλ₯Ό κ°μ§ λ΄μ₯ κ°μ²΄μ΄λ€. (β» ν¨μ κ°μ²΄κ° μλλ€.)
Math λ Number μλ£νλ§ μ§μνλ€.
toFixed()
toFixed() λ©μλλ μ«μλ₯Ό κ³ μ μμμ μΌλ‘ νκΈ°ν΄ λ°ννλ€.
const pi = 3.14159265358979
console.log(pi)
const str = pi.toFixed(2)
console.log(str)
console.log(typeof str)
μ€ν κ²°κ³Ό
3.14159265358979
3.14
string
toFixed() λ©μλλ₯Ό μ¬μ©νλ©΄ String μλ£νμ΄λ€.
parseInt() / parseFloat()
parseInt() ν¨μλ λ¬Έμμ΄ μΈμλ₯Ό νμ±νμ¬ νΉμ μ§μμ μ μλ₯Ό λ°ννλ€.
parseFloat() ν¨μλ μ£Όμ΄μ§ κ°μ νμν κ²½μ° λ¬Έμμ΄λ‘ λ³νν ν λΆλμμμ μ€μλ‘ νμ±ν΄ λ³ννλ€.
const integer = parseInt(str)
const float = parseFloat(str)
console.log(integer)
console.log(float)
console.log(typeof integer, typeof float)
μ€ν κ²°κ³Ό
3
3.14
number number
String μλ£νμμ number μλ£νμΌλ‘ λ³νλλ€.
Math Methods
Math.abs() : μ«μμ μ λκ°μ λ°ννλ€.
Math.min() : 0κ° μ΄μμ μΈμμμ μ μΌ μμ μλ₯Ό λ°ννλ€.
Math.max() : 0κ° μ΄μμ μΈμμμ μ μΌ ν° μλ₯Ό λ°ννλ€.
Math.ceil() : μ£Όμ΄μ§ μ«μλ³΄λ€ ν¬κ±°λ κ°μ μ«μ μ€ κ°μ₯ μμ μ«μλ₯Ό integer(μ μ)λ‘ λ°ννλ€. (μ¬λ¦Ό)
Math.floor() : μ£Όμ΄μ§ μ«μμ κ°κ±°λ μμ μ μ μ€μμ κ°μ₯ ν° μλ₯Ό λ°ννλ€. (λ΄λ¦Ό)
Math.round() : μ λ ₯κ°μ λ°μ¬λ¦Όν μμ κ°μ₯ κ°κΉμ΄ μ μ κ°μ λ°ννλ€.
Math.random() : 0κ³Ό 1 μ¬μ΄μ λμλ₯Ό λ°ννλ€.
console.log('abs: ', Math.abs(-12))
console.log('min: ', Math.min(2, 8))
console.log('max: ', Math.max(2, 8))
console.log('ceil: ', Math.ceil(3.14))
console.log('floor: ', Math.floor(3.14))
console.log('round: ', Math.round(3.14))
console.log('round: ', Math.round(3.5))
console.log('random: ', Math.random())
μ€ν κ²°κ³Ό
abs: 12
min: 2
max: 8
ceil: 4
floor: 3
round: 3
round: 4
random: 0.7236102943712774
'π€ Language > π¨ JavaScript' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
JS. λ°°μ΄(2) / Array (0) | 2022.02.21 |
---|---|
JS. λ°°μ΄ (1) / Array (0) | 2022.02.20 |
JS. λ¬Έμ (0) | 2022.02.20 |
JS. μμ(νμ₯) (0) | 2022.02.15 |
JS. ES6 Classes (0) | 2022.02.15 |