[JavaScript] Math
νκ·Έ: javascript · Math · ceil · floor · round · toFixed random · max · min · abs · pow · sqrt
1. Math
Math
λ μνμ μΈ μμμ ν¨μλ₯Ό μν μμ±κ³Ό λ©μλλ₯Ό κ°μ§ λ΄μ₯ κ°μ²΄
[Math.ceil() - JavaScript | MDN](https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil) |
- Math.ceil(x)
μ£Όμ΄μ§ μ«μλ³΄λ€ ν¬κ±°λ κ°μ μ«μ μ€ κ°μ₯ μμ μ«μλ₯Ό integerλ‘ λ°ν.
- 맀κ°λ³μ : μ«μ
- μ¬λ¦Ό
- μ μλ‘ λ°ν
- μμμ μλ¦Ώμμ μκ΄μμ΄ μμ μ리μ κ°μ΄ μλ€λ©΄ μ¬λ¦¬κ±°λ λ΄λ¦°λ€.
let num1 = 5.1;
let num2 = 5.7;
Math.ceil(num1);
console.log(Math.ceil(num1));
Math.ceil(num2);
console.log(Math.ceil(num2));
=> κ²°κ³Όκ°
[Running] node "/workspaces/codespaces-blank/index.js"
6
6
[Done] exited with code=0 in 0.049 seconds
- Math.floor(x)
Math.floor() ν¨μλ Math.floor()
ν¨μλ μ£Όμ΄μ§ μ«μμ κ°κ±°λ μμ μ μ μ€μμ κ°μ₯ ν° μλ₯Ό λ°ν
[Math.floor() - JavaScript | MDN](https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Math/floor) |
- 맀κ°λ³μ : μ«μ
- λ΄λ¦Ό
- μ μλ‘ λ°ν
- μμμ μλ¦Ώμμ μκ΄μμ΄ μμ μ리μ κ°μ΄ μλ€λ©΄ μ¬λ¦¬κ±°λ λ΄λ¦°λ€.
let num1 = 5.1;
let num2 = 5.7;
Math.floor(num1);
console.log(Math.floor(num1));
Math.floor(num2);
console.log(Math.floor(num2));
=>κ²°κ³Όκ°
[Running] node "/workspaces/codespaces-blank/index.js"
5
5
[Done] exited with code=0 in 1.421 seconds
- Math.round(x)
Math.round()
Β ν¨μλ μ
λ ₯κ°μ λ°μ¬λ¦Όν μμ κ°μ₯ κ°κΉμ΄ μ μ κ°μ λ°ν
[Math.round() - JavaScript | MDN](https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Math/round) |
- 맀κ°λ³μ : μ«μ
- λ°μ¬λ¦Ό
- μ μλ‘ λ°ν
let num1 = 5.1;
let num2 = 5.7;
console.log(Math.round(num1));
console.log(Math.round(num2));
=> κ²°κ³Όκ°
[Running] node "/workspaces/codespaces-blank/index.js"
5
6
[Done] exited with code=0 in 0.061 seconds
- toFixed()
μ«μλ₯Ό κ³ μ μμμ νκΈ°λ²(fixed-point notation)μΌλ‘ νμ
[Number.prototype.toFixed() - JavaScript | MDN](https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed) |
- 맀κ°λ³μ : μ«μ
- μμμ μ
μ§Έ μ리μμ λ°μ¬λ¦Όμ μν κ²½μ°Β
toFixed(n)
Β λ©μλλ₯Ό μ¬μ©ν¨
let myNumber = 58.112512512421424;
console.log(Number(myNumber.toFixed(2)));
=>κ²°κ³Όκ°
[Running] node "/workspaces/codespaces-blank/index.js"
58.11
[Done] exited with code=0 in 0.049 seconds
- Math.random(x)
Math.random()
Β ν¨μλ 0 μ΄μ 1 λ―Έλ§μ ꡬκ°μμ κ·Όμ¬μ μΌλ‘ κ· μΌν(approximately uniform) λΆλμμ«μ μμ¬λμλ₯Ό λ°ννλ©°, μ΄ κ°μ μ¬μ©μκ° μνλ λ²μλ‘ λ³νν μ μλ€. λμ μμ± μκ³ λ¦¬μ¦μ μ¬μ©λλ μ΄κΈ°κ°μ ꡬνμ²΄κ° μ ννλ©°, μ¬μ©μκ° μ ννκ±°λ μ΄κΈ°νν μ μλ€.
*μ°Έκ³ : Β Math.random()
μ μνΈνμ μΌλ‘ μμ ν λμλ₯ΌΒ μ 곡νμ§ μμΌλ―λ‘, 보μκ³Ό κ΄λ ¨λ μ΄λ€ κ²μλ μ΄ ν¨μλ₯Ό μ¬μ©ν΄μλ μ λλ€. κ·Έ λμ Web Crypto APIμΒ window.crypto.getRandomValues()Β λ©μλλ₯Ό μ΄μ©νμ¬μΌ νλ€.
[Math.random() - JavaScript | MDN](https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Math/random) |
- 맀κ°λ³μ : μ«μ
- 0 μ΄μ 1 λ―Έλ§μ λΆλμμ«μ μμ¬ λμ.
const num =Math.random()
console.log('0~10 μ¬μ΄μ λλ€ν μ«μ:',num*10)
console.log('0~50 μ¬μ΄μ λλ€ν μ«μ:',num*50)
=>κ²°κ³Όκ°
[Running] node "/workspaces/codespaces-blank/index.js"
0~10 μ¬μ΄μ λλ€ν μ«μ: 2.2020552161617157
0~50 μ¬μ΄μ λλ€ν μ«μ: 11.010276080808579
[Done] exited with code=0 in 0.048 seconds
- Math.max(x)
Math.max()Β ν¨μλ μ λ ₯κ°μΌλ‘ λ°μ 0κ° μ΄μμ μ«μ μ€ κ°μ₯ ν° μ«μλ₯Ό λ°ν
[Math.max() - JavaScript | MDN](https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Math/max) |
- 맀κ°λ³μ : κ°1, κ°2, β¦
- μ λ ₯λ μ«μ μ€ κ°μ₯ ν° μ«μλ₯Ό λ°νν©λλ€. λ§μ½ μΈμ μ€ νλλΌλ μ«μλ‘ λ³ννμ§ λͺ»νλ€λ©΄Β NaNλ‘ λ°ν
console.log(Math.max(1, 4, 10, -1, 2.24))
=>κ²°κ³Όκ°
[Running] node "/workspaces/codespaces-blank/index.js"
10
[Done] exited with code=0 in 0.05 seconds
- Math.min(x)
Math.min()
Β ν¨μλ μ£Όμ΄μ§ μ«μλ€ μ€ κ°μ₯ μμ κ°μ λ°ν
[Math.min() - JavaScript | MDN](https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Math/min) |
- 맀κ°λ³μ : κ°1, κ°2, β¦
- μ λ ₯λ μ«μ μ€ κ°μ₯ μμ μ«μλ₯Ό λ°νν©λλ€. λ§μ½ μΈμ μ€ νλλΌλ μ«μλ‘ λ³ννμ§ λͺ»νλ€λ©΄Β NaNλ‘ λ°ν
console.log(Math.min(1, 4, 10, -1, 2.24))
=>κ²°κ³Όκ°
[Running] node "/workspaces/codespaces-blank/index.js"
-1
[Done] exited with code=0 in 0.055 seconds
- Math.abs(x)
Math.abs()
Β ν¨μλ μ£Όμ΄μ§ μ«μμ μ λκ°μ λ°νν©λλ€.Β x
κ° μμμ΄κ±°λ 0μ΄λΌλ©΄Β x
λ₯Ό 리ν΄νκ³ ,Β x
κ° μμλΌλ©΄Β x
μ λ°λκ°, μ¦ μμλ₯Ό λ°ν
[Math.abs() - JavaScript | MDN](https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Math/abs) |
- 맀κ°λ³μ : μ«μ
- μ£Όμ΄μ§ μ«μμ μ λκ°.
- λΉ κ°μ²΄, νλ μ΄μμ μμλ₯Ό κ°μ§ λ°°μ΄, μ«μκ° μλ λ¬Έμμ΄,undefinedλ λΉ λ§€κ°λ³μλ₯Ό λ°μΌλ©΄ NaNμ λ°νν©λλ€.null, λΉ λ¬Έμμ΄μ΄λ λΉ λ°°μ΄μ μ 곡νλ©΄ 0μ λ°ν
function difference(a, b) {
return Math.abs(a - b);
}
console.log(difference(3, 5));
=> κ²°κ³Όκ°
[Running] node "/workspaces/codespaces-blank/index.js"
2
[Done] exited with code=0 in 0.196 seconds
- Math.pow(base,exponent)
Math.pow()
ν¨μλbase^exponent
μ²λΌΒ base
μΒ exponent
λ₯Ό μ κ³±ν κ°μ λ°ν
[Math.pow() - JavaScript | MDN](https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Math/pow) |
- 맀κ°λ³μ : base(λ° κ°) , exponent(μ§μ)
- μ£Όμ΄μ§ λ° κ°μ μ£Όμ΄μ§ μ§μ κ°μΌλ‘ κ±°λμ κ³±ν μ«μ κ°
console.log(Math.pow(7, 3));
console.log(Math.pow(7, -2));
console.log(Math.pow(-7, 0.5));
=> κ²°κ³Όκ°
[Running] node "/workspaces/codespaces-blank/index.js"
343
0.020408163265306124
NaN
[Done] exited with code=0 in 0.071 seconds
- Math.sqrt(x)
Math.sqrt()
ν¨μλ μ«μμ μ κ³±κ·Όμ λ°ν
[Math.sqrt() - JavaScript | MDN](https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Math/sqrt) |
- 맀κ°λ³μ : μ«μ
- μ£Όμ΄μ§ μ«μμ 루νΈ(βΒ )λ₯Ό μμλλ€. λ§μ½ μ«μκ° μμμ΄λ©΄Β NaNλ₯Ό λ°ν
console.log(Math.sqrt(16));
console.log(Math.sqrt(2));
console.log(Math.sqrt(-1));
=> κ²°κ³Όκ°
[Running] node "/workspaces/codespaces-blank/index.js"
4
1.4142135623730951
NaN
[Done] exited with code=0 in 0.048 seconds