working with NUMBERS in JS Part 3 - number methods.

working with NUMBERS in JS Part 3 - number methods.

Useful number methods in JavaScript.

Β·

3 min read

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.

Β