자바스크립트의 숫자 형식 메서드는 모두 숫자가 아니라 문자열을 반환합니다. 해당 형식에 필
요한 각종 기호를 온전히 표현하려면 반드시 문자열이어야 하기 때문입니다(물론 이런 문자
열은 쉽게 숫자로 바꿀 수 있습니다). 따라서 숫자 형식을 바꾸는 건 실제로 표시하기 직전에
해야 합니다. 숫자를 저장하거나 계산할 때는 따로 형식을 지정하지 않은 숫자 타입이어야 합
니다.
16.1.1
고정 소수점
소수점 뒤 자릿수를 지정하는 형식을 원한다면
toFixed
()
를 사용합니다.
const
x
=
19
.
51
;
x
.
toFixed
(
3
); // "
19
.
510
"
x
.
toFixed
(
2
); // "
19
.
51
"
x
.
toFixed
(
1
); // "
19
.
5
"
x
.
toFixed
(
0
); // "
20
"
이 숫자는 버림
truncation
이 아니라 반올림
round
입니다.
16.1.2
지수 표기법
지수 표기법이 필요할 때는
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.
O’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
I wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
I’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
I'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.