Sunday, February 23, 2020

How to implement the various JavaScript Number methods with examples


Hello guys, morning and welcome to my new tutorial post on JavaScript. In my previous tutorial post we discussed about how to implement the various JavaScript Number properties with examples and I believe you guys understood the in-depth explanations given.

In this tutorial post we will be discussing about JavaScript Number Methods. But before i continue am urging you guys to make this blog an interactive blog, ask questions via the comment box or even use the chat box to ask questions or interact among yourselves. So let's get started with the first Number Method.

Number - toExponential()

This method returns a string representing the number object in exponential notation.

Syntax

Its syntax is as follows -

number.toExponential( [fractionDigits] )

Parameter Details

fractionDigits - An integer specifying the number of digits after the decimal point. Defaults to as many digits as neccessary to specify the number.

Return Value

A string representing a Number object in exponential notation with one digit before the decimal point, rounded to fractionDigits digits after the decimal point. If the fractionDigits argument is omitted, the number of digits after the decimal point defaults to the number of digits necessary to represent the value uniquely.

Example

Try the following example

<html>
     <head>
          <title>JavaScript Method toExponential()</title>
     </head>

     <body>
          <script  type = "text/Javascript">
               var num = 77.1234;
               var val = num.toExponential();
               document.write("num.toExponential() is : " + val);         
               document.write("<br />");

               val = num.toExponential(4);
               document.write("num.toExponential(4) is : " + val);
               document.write("<br />");

               val = num.toExponential(2);
               document.write("num.toExponential(2) is : " + val);
               document.write("<br />");

               val = 77.1234.toExponential();
               document.write("77.1234.toExponential() is : " + val);         
               document.write("<br />");

               val = 77.1234.toExponential();
               document.write("77.toExponential() is : " + val);
          </script>
     </body>
</html>

Output

Below is the output of the above example

num.toExponential() is : 7.71234e+1
num.toExponential(4) is : 7.7123e+1
num.toExponential(2) is : 7.71e+1
77.1234.toExponential() is : 7.71234e+1
77.toExponential() is : 7.71234e+1

You can also read our tutorial post on: Css outline 


Number - toFixed()

This method formats a number with a specific number of digits to the right of the decimal.

Syntax

Its syntax is as follows -

number.toFixed( [Digits] )

Parameter Details

digits - The number of digits to appear after the decimal point.

Return Value

A string representation of number that does not use exponential notation and has the exact number of digits after the decimal place.

Example

Try the following example

<html>
     <head>
          <title>JavaScript toFixed() Method</title>
     </head>

     <body>
          <script  type = "text/Javascript">
               var num = 177.1234;
               var val = num.toFixed();
               document.write("num.toFixed() is : " + val);         
               document.write("<br />");

               val = num.toFixed(6);
               document.write("num.toFixed(6) is : " + val);
               document.write("<br />");

               val = num.toFixed(1);
               document.write("num.toFixed(1) is : " + val);
               document.write("<br />");

               val = (1.23e+20).toFixed(2);
               document.write("(1.23e+20).toFixed(2) is : " + val);          
               document.write("<br />");

               val = (1.23e-10).toFixed(2);
               document.write("(1.23e-10).toFixed(2) is : " + val);
          </script>
     </body>
</html>

Output

Below is the output of the above example 

num.toFixed() is : 177
num.toFixed(6) is : 177.123400
num.toFixed(1) is : 177.1
(1.23e+20).toFixed(2) is : 123.000000000000000000.00
(1.23e-10).toFixed(2) is : 0.00

You can also read our tutorial post on: Css cursor


Number - toLocaleString()

This method converts a number object into a human readable string representing the number using the locale of the environment.

Syntax

Its syntax is as follows -

number.toLocaleString()

Return Value

Returns a human readable string representing the number using the locale of the environment.

Example

Try the following example

<html>
     <head>
          <title>JavaScript toLocaleString() Method</title>
     </head>

     <body>
          <script  type = "text/Javascript">
               var num = new Number(177.1234);
               document.write("num.toLocaleString() is : " + num.toLocaleString() );         
          </script>
     </body>
</html>

Output

Below is the output of the above example

num.toLocaleString() is : 177.1234

You can also read our tutorial post on: Css Images


Number - toPrecision

This method returns a string representing the number object to the specified precision.

Syntax

Its syntax is as follows -

number.toPrecision( [precision] )

Parameter Details

precision - An integer specifying the number of significant digits.

Return Value

Returns a string representing a Number object in fixed-point or exponential notation rounded toprecision significant digits.

Example

Try the following example

<html>
     <head>
          <title>JavaScript toPrecision() Method</title>
     </head>

     <body>
          <script  type = "text/Javascript">
               var num = new Number(7.123456); 
               document.write("num.toPrecision() is : " + num.toPrecision() );         
               document.write("<br />");

               document.write("num.toPrecision(4) is : " + num.toPrecision(4) );
               document.write("<br />");

               document.write("num.toPrecision(2) is : " + num.toPrecision(2) );
               document.write("<br />");

               document.write("(num.toPrecision(1) is : " + num.toPrecision(1) );          
          </script>
     </body>
</html>

Output

Below is the output of the above example

num.toPrecision() is : 7.123456
num.toPrecision(4) is : 7.123
num.toPrecision(2) is : 7.1
num.toPrecision(1) is : 7

You can also read our tutorial post on: Css Fonts


Number - toString()
This method returns a string representing a specified object. The toString() method parses its first argument and attemps to return a string representation in the specified radix(base).

Syntax
Its syntax is as follows -

number.toString( [radix] )

Parameter Details
radix - An integer between 2 and 36 specifying the base to use for representing numeric values.

Return Value
Returns a string representing the specified Number object.

Example
Try the following example

<html>
     <head>
          <title>JavaScript toString() Method</title>
     </head>

     <body>
          <script  type = "text/Javascript">
               var num = new Number(15); 
               document.write("num.toString() is : " + num.toString() );         
               document.write("<br />");

               document.write("num.toString(2) is : " + num.toString(2) );
               document.write("<br />");

               document.write("num.toString(4) is : " + num.toString(4) );     
          </script>
     </body>
</html>

Output
Below is the output of the above example

num.toString() is : 15
num.toString(2) is : 1111
num.toString(4) is : 33

You can also read our tutorial post on: Html Javascript


Number - valueOf()
This method returns the primitive value of the specified number object.

Syntax
Its syntax is as follows -

number.valueOf()

Return Value
Returns the primitive value of the specified number object.

Example
Try the following example

<html>
     <head>
          <title>JavaScript valueOf() Method</title>
     </head>

     <body>
          <script  type = "text/Javascript">
               var num = new Number(15.11234); 
               document.write("num.valueOf() is : " + num.valueOf() );            
          </script>
     </body>
</html>

Output
Below is the output of the above example

num.valueOf() is : 15.11234

Alright guys, we have come to the end of this tutorial on JavaScript Number methods. In my next tutorial post, we will be discussing JavaScript Boolean Object so stay tuned.

Feel free to drop your questions via the comment box and they will be attended to as soon as possible. Follow us on our various social media platforms to stay updated with our latest tutorials. Thanks for reading and bye for now
Share:

1 comment:

  1. The article is so appealing. You should read this article before choosing the Automated big data engineering you want to learn.

    ReplyDelete