working with NUMBERS in JS Part 3 - number methods.
Useful number methods in JavaScript.
Hello ππΎ everyone, Welcome, in this post weβll continue our learning about data types in JavaScript specifically number type. In the part2 we discussed comparing numbers with JavaScript. This post discusses some useful number methods available in JS.
Introduction
There is a set of built-in actions that can be performed on numbers ie . These actions are called methods. The following paragraphs will discuss briefly some of the most useful number methods in JavaScript mainly by illustrating how they work.
The toString() method
Here is how the toString([radix])
method works:
It represents a number as a string (the type of the data is changed to a string)
let num = 17; let res = num.toString(); typeOf(res); //-> string console.log(res); //-> β17β
It takes a single optional parameter which specifies the base(radix) the result should be represented in.
When the base parameter is not specified, by default the string representation of the number is done in base 10.
For example:
let num = 17;
let res = num.toString();
console.log(res); //-> β17β
// in base 2
let res2 = mum.toString(2);
console.log(res2); //-> β17β
// in base 8
let res8 = mum.toString(8);
console.log(res8); //-> β17β
toExponential() method
Here is how the toExponential()
method works:
It represents a number in the exponential form and returns it as a string.
It takes a single optional parameter which specifies the the number of decimal places the exponential form should contain. When omitted, it represents the number in up to 16 decimal places.
let num = 12.483;
let expo = num.toExponential(2);
console.log(expo);
//-> β1.25e+1β
typeOf(expo);
//-> String
What will num.toExponent(3)
result in? You may find it challenging to understand this method if you do not have a solid understanding of exponential form in maths. If so you should revise the topic before taking a look at this method.
The toFixed() method
Hereβs how the toFixed() method works:
It rounds a number to a specified number of decimal places (fixed point notation) and returns the value as a string.
It accepts a single parameter which determines the number of decimal places. When omitted it defaults to 0
let num = 12.6748;
let result = num.toFixed();
//-> β13β
let result2 = num.toFixed(1);
//-> β12.7β
let result3 = num.toFixed(2);
//-> β12.67β
It is very common to use two decimal places when working with moneyπ°.
If more decimal places are required than specified in the original number the toFixed() method will pad it with additional 0βs.
The toPrecision() method
Hereβs how the toPrecision() method works:
it formats a number to a specified length/number of significant digits and returns the value as a string.
it accepts a parameter which determines the number significant digits the returned value should contain.
let num = 12.6748;
let res = num.toPrecision(1);
//-> β1e+1β
let res2 =num.toPrecision(2);
//-> β13β
let res3=num.toPrecision(10);
//-> β12.67480000β
let x = 0.00134;
let pres = x.toPrecision(2);
//-> 0.0013
Thatβs a wrap! Thank you for reading this post, hopefully it was useful. Kindly share your thoughts in the comment section. Click the follow button for useful posts on learning and mastering JavaScript as well as other technologies in web development!.
Ask me anything on Twitter at @a__sally.