This page was translated from English by the community.Learn more and join the MDN Web Docs community.
Number.prototype.toPrecision()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015년 7월.
toPrecision() 메서드는Number 객체를 지정된 정밀도로 나타내는 문자열을 반환한다.
In this article
시도해 보기
function precise(x) { return x.toPrecision(4);}console.log(precise(123.456));// Expected output: "123.5"console.log(precise(0.004));// Expected output: "0.004000"console.log(precise(1.23e5));// Expected output: "1.230e+5"문법
js
numObj.toPrecision([precision]);파라미터
precision선택적 파라미터. 유효 자릿수를 지정하는 정수.
반환 값
고정 소수점 또는 지수 표기법의Number 객체를 정밀 유효 숫자로 반올림 한 문자열입니다.toPrecision()에도 적용되는Number.prototype.toFixed() 메서드에 대한 설명에서 반올림에 대한 설명을 참조하세요.
precision(정밀도) 인수가 생략되면Number.prototype.toString()처럼 작동합니다.precision(정밀도) 인수가 정수가 아닌 값이면 가장 가까운 정수로 반올림됩니다.
예외
RangeErrorprecision(정밀도)가 1에서 100 사이가 아닌 경우RangeError가 발생합니다. 구현은 더 큰 값과 더 작은 값을 지원할 수 있습니다. ECMA-262는 최대 21 자리의 정밀도 만을 요구합니다.
예제
>toPrecision 사용
js
var numObj = 5.123456;console.log(numObj.toPrecision()); // logs '5.123456'console.log(numObj.toPrecision(5)); // logs '5.1235'console.log(numObj.toPrecision(2)); // logs '5.1'console.log(numObj.toPrecision(1)); // logs '5'numObj = 0.000123;console.log(numObj.toPrecision()); // logs '0.000123'console.log(numObj.toPrecision(5)); // logs '0.00012300'console.log(numObj.toPrecision(2)); // logs '0.00012'console.log(numObj.toPrecision(1)); // logs '0.0001'// 일부 상황에서는 지수 표기법이 반환 될 수 있습니다console.log((1234.5).toPrecision(2)); // logs '1.2e+3'명세
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-number.prototype.toprecision> |