When you hear of “number methods,” we are simply referring to some built-in functions for numbers in JavaScript. Also, you can call these methods into your codes to quickly carry out an operation that would have taken you time to code manually. And you can use these functions on any number when coding with JavaScript.
Read on to learn about some of these numerical methods in javascript.
The toExponential() Method
The result of the toExponential() method is a string that has a rounded number in exponential notation. Again, the parameter specifies how many characters come after the decimal point.
// The number is rounded and expressed in exponential notation in a string by the toExponential() method's return value.
let x0 = 8.656;
let x00 = x0.toExponential(2);
console.log(x00) //output is 66e+0
let x1 = 7.976;
let x11 = x1.toExponential(4);
console.log(x11) //output is 9760e+0
let x2 = 7.976;
let x22 = x2.toExponential(6);
console.log(x22) //output is 976000e+0
The toString() Method
A number is returned as a string by the toString() method. For example:
let x_n_s = 5632;
// This will change the int value to a string
let x_n_s1 = x_n_s.toString();
console.log(x_n_s1) // output 5632
// we use the typeof to check the output new form
console.log(typeof x_n_s1) // output string
The toPrecision() Method
A string with a number and a custom length can be returned by programs using the toPrecision() function. However, this suggests that you can choose to either compress or expand the result of your calculations. For example:
// this code will resize the number of floats you have as your return value or output
let x1 = 9.656;
let x2 = 9.656;
let y0 = x1 * x2;
console.log(y0.toPrecision()); // output......93.23833600000002
console.log(y0.toPrecision(2)); // output.........93
console.log(y0.toPrecision(3)); // output.........93.2
console.log(y0.toPrecision(4)); // output.........93.24
console.log(y0.toPrecision(5)); // output.........93.238
console.log(y0.toPrecision(8)); // output.........93.238336
console.log(y0.toPrecision(19)); // output.........93.23833600000001809
The toFixed() Method
You can use the toFixed() function to ensure that a program returns a string that contains the number written with the number of decimals you want. For example:
// this code will resize the number of floats you have as your return value or output
let x11 = 9.656;
let x22 = 9.656;
let y00 = x11 * x22;
// A number is rounded to a specified number of digits using the toFixed() method.
console.log(y00.toFixed()); // output......93.
console.log(y00.toFixed(2)); // output.........93.24
console.log(y00.toFixed(3)); // output.........93.238
console.log(y00.toFixed(4)); // output.........93.2383
console.log(y00.toFixed(5)); // output......... 93.23834
console.log(y00.toFixed(8)); // output......... 93.23833600
console.log(y00.toFixed(19)); // output......... 93.2383360000000180889
How to Convert Variables to Numbers
JavaScript allows for the conversion of variables to numbers. And there are many ways to get this done, but the simplest way to do it is by using three popular built-in JS functions. These functions include:
1. The Number() Method
The Number() method is the first in the list of functions that makes it simple to convert variables into numbers. Again, this method converts JavaScript variables to numbers. Example:
// the code below will convert the following parameters in the number function to numbers
console.log(Number(true)); // output......1
console.log(Number(false)); // output......2
console.log(Number("54")); // output......54
console.log(Number(" 112")); // output......112
console.log(Number("53 ")); // output......53
console.log(Number(" 23 ")); // output......23
console.log(Number("11.3")); // output......11.3
console.log(Number("15,53")); // output......NaN
console.log(Number("Anna")); // output......NaN
console.log(Number("Rick")); // output......NaN
2. The parseFloat() Method
The next JavaScript function you can use for this purpose is parseFloat(). So, only the first number is returned after this method parses a string and returns a number.
// the code below will convert strings to numbers
console.log(parseFloat("54")); // output......54
console.log(parseFloat("112.54")); // output......112.54
console.log(parseFloat("53 45 83")); // output......53
console.log(parseFloat(" 23 years ")); // output......23
console.log(parseFloat("11.323")); // output......11.323
console.log(parseFloat("15,53")); // output......15
console.log(parseFloat("Rick")); // output......NaN
3. The parseInt() Method
The parseInt() method converts strings to whole numbers or integers and provides a whole number as a result.
console.log( parseInt("54")); // output......54
console.log( parseInt("112.54")); // output......NaN112
console.log( parseInt("53 45 83")); // output......53
console.log( parseInt(" 23 years ")); // output......23
console.log( parseInt("11.323")); // output......11
console.log( parseInt("15,53")); // output......15
console.log( parseInt("Rick")); // output......NaN
JavaScript Number Properties
NaN (not a number) and prototype are the two most frequent JavaScript properties that you might occasionally encounter. While NaN is one of the most frequently used properties, especially when working with numbers and strings, the prototype allows you to add new properties and methods to a Number object.
Let’s look at some of the other properties of JS.
Properties | Descriptions |
EPSILON | Depicts the difference between a JavaScript small number and 1 |
POSITIVE_INFINITY | Refer to all JavaScript positive infinity values |
NEGATIVE_INFINITY | Refer to all JavaScript negative infinity values |
MAX_VALUE | JavaScript biggest number |
MIN_VALUE | JavaScript smallest number |
MAX_SAFE_INTEGER | Maximum safe integer (253 – 1) |
MIN_SAFE_INTEGER | Minimum safe integer (-(253 – 1)) |
Conclusion
You learned about JavaScript number methods and properties and how to use them in this article. So, to make the learning process simple, we provided examples. Additionally, ensure you practice more; you can read the documentation about JavaScript numbers at your leisure.