Skip to content

Instantly share code, notes, and snippets.

@justxor
Created May 31, 2022 07:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justxor/4f7df3340b0dfa155e80564df975a0cb to your computer and use it in GitHub Desktop.
Save justxor/4f7df3340b0dfa155e80564df975a0cb to your computer and use it in GitHub Desktop.
var numObj = 5.123456;
console.log(numObj.toPrecision()); // выведет '5.123456'
console.log(numObj.toPrecision(5)); // выведет '5.1235'
console.log(numObj.toPrecision(2)); // выведет '5.1'
console.log(numObj.toPrecision(1)); // выведет '5'
numObj = 0.000123;
console.log(numObj.toPrecision()); // выведет '0.000123'
console.log(numObj.toPrecision(5)); // выведет '0.00012300'
console.log(numObj.toPrecision(2)); // выведет '0.00012'
console.log(numObj.toPrecision(1)); // выведет '0.0001'
// Обратите внимание, что если заданного количества разрядов
// недостаточно для точного отображения целой части числа,
// значение может быть возвращено в экспоненциальной записи.
console.log((1234.5).toPrecision(2)); // выведет '1.2e+3'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment