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 July 2015.
Die toPrecision()
-Methode von Number
-Werten gibt einen String zurück, der diese Zahl mit der angegebenen Anzahl signifikanter Stellen darstellt.
Probieren Sie es aus
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"
Syntax
toPrecision()
toPrecision(precision)
Parameter
precision
Optional-
Eine ganze Zahl, die die Anzahl signifikanter Stellen angibt.
TypeError
-
Wird ausgelöst, wenn diese Methode auf ein Objekt angewendet wird, das kein
Number
ist.
Rückgabewert
Ein String, der die gegebene Zahl unter Verwendung der angegebenen Anzahl signifikanter Stellen darstellt. Wissenschaftliche Notation wird verwendet, wenn der Exponent größer oder gleich precision
ist oder kleiner als -6. Verhält sich genauso wie Number.prototype.toString()
, wenn das Argument precision
weggelassen wird.
Ausnahmen
RangeError
-
Wird ausgelöst, wenn
precision
nicht zwischen1
und100
(einschließlich) liegt.
Beispiele
Verwendung von toPrecision
// This number has exponent 0, so it will never use exponential notation
let num = 5.123456;
console.log(num.toPrecision()); // '5.123456'
console.log(num.toPrecision(5)); // '5.1235'
console.log(num.toPrecision(2)); // '5.1'
console.log(num.toPrecision(1)); // '5'
// This number has exponent -4, so it will never use exponential notation
num = 0.000123;
console.log(num.toPrecision()); // '0.000123'
console.log(num.toPrecision(5)); // '0.00012300'
console.log(num.toPrecision(2)); // '0.00012'
console.log(num.toPrecision(1)); // '0.0001'
// This number has exponent 3, so it will use exponential notation if precision is less than 4
num = 1234.5;
console.log(num.toPrecision(1)); // '1e+3'
console.log(num.toPrecision(2)); // '1.2e+3'
console.log(num.toPrecision(6)); // '1234.50'
// This number has exponent -7, so it will always use exponential notation
num = 0.00000012345;
console.log(num.toPrecision(1)); // '1e-7'
console.log(num.toPrecision(10)); // '1.234500000e-7'
Spezifikationen
Specification |
---|
ECMAScript® 2025 Language Specification # sec-number.prototype.toprecision |
Browser-Kompatibilität
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
toPrecision |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support