5 Useful JS Number Methods

Hi everybody, I’m Aya Bouchiha, todya, we will discuss 5 useful Number Object Methods toFixed() and isInteger().

Number.prototype.toFixed()

toFixed(digits) returns as a string the specified number rounded to a given number of decimals.


This content originally appeared on DEV Community and was authored by Aya Bouchiha

Hi everybody, I'm Aya Bouchiha, todya, we will discuss 5 useful Number Object Methods toFixed() and isInteger().

Number.prototype.toFixed()

  • toFixed(digits) returns as a string the specified number rounded to a given number of decimals.
const price = 742;
console.log(price.toFixed(2)) // 742.00
console.log(20.248.toFixed(1)) // 20.2
console.log(Math.PI.toFixed(3)) // 3.142

Number.isInteger()

  • isInteger(num): is a static method used to check if the given value is an integer.
console.log(Number.isInteger(-1)) // true
console.log(Number.isInteger(400.00)) // true
console.log(Number.isInteger(657.1540)) // false
console.log(Number.isInteger(Math.PI)) // false

Number.isNaN()

isNaN(num) : is a static method used to check if the given value is not a number

console.log(Number.isNaN(1)); // false
console.log(Number.isNaN('1')); // false
console.log(Number.isNaN('Aya Bouchiha')); // false
console.log(Number.isNaN("")); // false
console.log(Number.isNaN(" ")); // false
console.log(Number.isNaN(Number.NaN)); // true
console.log(Number.isNaN(NaN)); // true
console.log(Number.isNaN('NaN')); // false
console.log(Number.isNaN(0 / 0)); // true
console.log(Number.isNaN(undefined)); // false
console.log(Number.isNaN(null)); // false
console.log(Number.isNaN([])); // false
console.log(Number.isNaN(true)); // false

Number.prototype.toPrecision()

toPrecision(precision): this method formats the specified number to a the given precision Where 1 <= precision <= 100

const pi = Math.PI;
console.log(pi.toPrecision()) //3.141592653589793
console.log(pi.toPrecision(1)) // 3
console.log(pi.toPrecision(3)) // 3.14
console.log(pi.toPrecision(101)) // error

Number.isFinite()

isFinite(num): is a static method that checks if the given number is finite.

console.log(Number.isFinite(1)) // true
console.log(Number.isFinite('10')) // false
console.log(Number.isFinite('Aya Bouchiha')) // false
console.log(Number.isFinite(Infinity)) // false
console.log(Number.isFinite(-Infinity))  // false
console.log(Number.isFinite(NaN)) // false

Summary

  • toFixed(digits) returns as a string the specified number rounded to a given number of decimals.

  • isInteger(num): checks if the given value is an integer.

  • isNaN(num): checks if the given value is not a number.

  • toPrecision(len): formats the specified number to the given precision.

  • isFinite(num): checks if the given number is finite.

References

To Contact Me:

Have a great day


This content originally appeared on DEV Community and was authored by Aya Bouchiha


Print Share Comment Cite Upload Translate Updates
APA

Aya Bouchiha | Sciencx (2021-07-27T00:51:39+00:00) 5 Useful JS Number Methods. Retrieved from https://www.scien.cx/2021/07/27/5-useful-js-number-methods/

MLA
" » 5 Useful JS Number Methods." Aya Bouchiha | Sciencx - Tuesday July 27, 2021, https://www.scien.cx/2021/07/27/5-useful-js-number-methods/
HARVARD
Aya Bouchiha | Sciencx Tuesday July 27, 2021 » 5 Useful JS Number Methods., viewed ,<https://www.scien.cx/2021/07/27/5-useful-js-number-methods/>
VANCOUVER
Aya Bouchiha | Sciencx - » 5 Useful JS Number Methods. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/27/5-useful-js-number-methods/
CHICAGO
" » 5 Useful JS Number Methods." Aya Bouchiha | Sciencx - Accessed . https://www.scien.cx/2021/07/27/5-useful-js-number-methods/
IEEE
" » 5 Useful JS Number Methods." Aya Bouchiha | Sciencx [Online]. Available: https://www.scien.cx/2021/07/27/5-useful-js-number-methods/. [Accessed: ]
rf:citation
» 5 Useful JS Number Methods | Aya Bouchiha | Sciencx | https://www.scien.cx/2021/07/27/5-useful-js-number-methods/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.