JS_Math.toFixed()

이서현·2022년 3월 18일
0

Math.toFixed()

  • 소수의 자릿수를 제한할 수 있다.
  • 인자로 숫자를 받아 문자열로 변환한다.
let num1 = 1012.2022;
console.log(num1.toFixed(1)); // 1012.2
console.log(num1.toFixed(2)); // 1012.20
console.log(num1.toFixed(3)); // 1012.202

console.log(typeof(num1.toFixed(1))); // string
  • 반올림이 된다.
let num2 = 1234.5678;
console.log(num2.toFixed()); // 1235
  • 인자를 지정하지 않으면 정수가 나온다.
let num3 = 1012.2022;
console.log(num3.toFixed()); // 1012
  • 소수의 자릿수 이상의 수를 인자로 넘기면 빈 공간을 0으로 채운다.
let num4 = 1012.2022;

console.log(num4.toFixed(10)); //1012.2022000000
profile
🌿💻💪🧠👍✨🎉

0개의 댓글