[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Number/toPrecision [Back]  [Original]

Number.prototype.toPrecision() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Number.prototype.toPrecision()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its 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() .

RangeError

precision() 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 2027 LanguageSpecification
# sec-number.prototype.toprecision

See also


Web Proxy Viewer  |  New URL  |  Original Page