Programming/JAVASCRIPT
Javascript 자바스크립트 소수점 자르기 toFixed
Beat.
2022. 7. 12. 08:02
Javascript 자바스크립트 소수점 자르기 toFixed
소수점을 자르기 위해서는 toFixed() 를 사용하시면 됩니다.
const num == 123.123456
console.log(num.toFixed()) // 123
console.log(num.toFixed(1)) // 123.1
console.log(num.toFixed(2)) // 123.12
console.log(num.toFixed(3)) // 123.123
const num2 = 0.123456
console.log(num2.toFixed()) // 0
console.log(num2.toFixed(1)) // 0.0
console.log(num2.toFixed(2)) // 0.01
console.log(num2.toFixed(3)) // 0.012
toFixed는 소수점 뒤에 나타낼 자리수이다.
0 ~ 20의 값을 사용할 수 있으며, 구현체에 따라 더 넓은 값을 지원할 수도 있습니다.
읽어주셔서 감사합니다.