We now have a youtube channel. Subscribe!

How to implement the various JavaScript Number methods with examples



Hello dear readers! welcome back to another section of my tutorial on JavaScript. In my last 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 guide, we are going to be discussing about JavaScript Number Methods.

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 been 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 to a human readable string that represents 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 that represents a specified object. The toString() method parses its first argument and attemps to return a string representation which is 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! This is where we are rounding up for this tutorial post. In my next tutorial, we are going to be discussing about the Javascript Boolean Object.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain