Loading...

Numbers method in JavaScript- Complete Guide 2022

Numbers method in JavaScript- Complete Guide 2022

Hey readers, in this article, we will be covering all numbers objects in JavaScript. Before jumping directly to the number methods, we will learn about JavaScript and the basics of what exactly a numbers object in JavaScript is along with different methods. So if you are new to all these concepts, don’t worry, we will be covering it right from the fundamentals and will then move to differences. So keep reading.

This blog post would cover a few important methods available in JavaScript to operate on numbers.

Number methods that are static

There are some static Number methods in JavaScript. Let’s take a look at a few of them.

isFinite(): Determines whether or not the provided value is a finite number.

isNaN(): This method determines whether or not the value supplied is NaN.

isInteger(): Determines whether or not the provided value is an integer.

isSafeInteger(): determines whether or not the provided value is a safe integer.

parseInt(string, [radix]): To convert a numeric string to an integer, use this method.

parseFloat(string): This method converts a floating-point number to a numeric floating string (number with a decimal point)

Examples

The parseInt() static function was used to parse or transform the numeric string ’06’ into a number 6 in the first example below.

In the second example, we used the static method is NaN() to check if the value assigned to a variable is a number.

We used the isInteger() static method in the third example below to check whether the value assigned to variable c is an integer.

// 1st const a = parseInt('06'); // it returns 6 as an integer. console.log(a); // 2nd const b = NaN; console.log(Number.isNaN(b)); // it returns true because NaN is not a number. // 3rd const c = 31; console.log(Number.isInteger(c)); // it returns true because b is an integer.
Code language: JavaScript (javascript)

Instance number methods

In JavaScript, there is a certain number of instance methods that act on a specific instance. Let’s have a look at some of them.

toFixed(fractionDigits): This method returns a string value for a number in fixed-point notation.

toExponential(fractionDigits): This method returns a string value for a number in exponential notation.

valueof(): This function returns the number’s real value.

toPrecision(precisionNumber) : This method returns a string value for a number with a specified precision or length.

toString(radix): It is used to convert numeric values into strings. Radix is optional in this case, which implies it is not required to be passed on. The radix is presumed to be 10 if radix or base are not specified in the function toString() { [native code] }() method.

 toLocaleString(): This function returns a string based on the locale settings of the browser.

Example

We used the toFixed() instance function to fix the value of c to only two digits in the first example below. As a result, it only returns 6.25.

We used the toExponential() instance function to transform the value of d into exponential form in the second example below. As a result, the value 254687 is calculated as 2.54687e+5.

To examine the value of variable b, we utilised the function valueOf() { [native code] }() instance function in the third example below. As a result, it returns 77.

We used the toPrecision() instance function in the fourth example below to get the number to a specified precision, which in this case is 2. As a result, the answer is 25.

The function toString() { [native code] }() instance function is used in the fifth example below to transform a number 45 into a string 45.

We utilised the function toLocaleString() { [native code] }() instance function to acquire the locale format of the given integer in the sixth example below. We’ve also supplied an object named fObj to specify the format, which in our instance is Indian Rupee, with hi-IN being the Hindi language-specific format (India). As a result, we got rupees 100.00 for 100.

// 1st let a = 6.2547; console.log(a.toFixed(2)); // this will return 6.25 // 2nd let b = 254687; let b_toExpo = b.toExponential(); console.log(b); // this will return 254687 console.log(b_toExpo); // this will return 2.54687e+5 // 3rd let c = 77; console.log(c.valueOf()); // this will return the value i.e. 77 // 4th let d = 25.4687; console.log(d.toPrecision(2));// this will return 25 // 5th let e = 45; console.log(e.toString()); // this will return a numeric string i.e. 45 as a String. // 6th var f = new Number(100); var fObj = { style: "currency", currency: "INR" // Indian Rupee } console.log(f.toLocaleString("hi-IN", fObj)); // Output will be ₹100.00
Code language: JavaScript (javascript)

Conclusion

This was all about the numbers in JavaScript. If you have any query related to React or JavaScript, do drop it down in the comment section also do check out codedamn courses if you want to learn more about JavaScript and React with its use cases and amazing projects. They also have an in-built playground for different programming languages and environment sets so do check that out and join codedamn’s community! Do join the community to get amazing discounts on courses as well as free codedamn membership and gift.

Hope you like it.

Sharing is caring

Did you like what mansi wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far