Friday 29 December 2017

Basic Javascript Number Methods

    Understanding some of the basic built-in javascript number methods

    This blog gives a brief overview of the basic javascript methods that are related to numbers.

    1. isNaN()

    Check whether a value is a number and returns true if not a number (NaN stands for Not a Number)
 
 <script type="text/javascript">
        //isNaN()
        document.write(isNaN(25)); //Output is false
        document.write(isNaN("This is a string")); //Output is true
        document.write(isNaN("77")); //Output is false
        document.write(isNaN("05/10/2016")); //Output is true
    </script>


    2. parseInt()

    Converts a string to an Integer. If string doesn't start with a valid number, it returns NaN

 <script type="text/javascript">
        //parseInt()
        document.write(parseInt(25)); //Output is 25
        document.write(parseInt("This is a string")); //Output is NaN
        document.write(parseInt("77.56")); //Output is 77
        document.write(parseInt("05/10/2016")); //Output is 5
    </script>


    3. parseFloat()
 
    Converts a string to an Float value. If string doesn't start with a valid number, it returns NaN

 <script type="text/javascript">
        //parseFloat()
        document.write(parseFloat(25)); //Output is 25
        document.write(parseFloat("This is a string")); //Output is NaN
        document.write(parseFloat("77.56")); //Output is 77.56
        document.write(parseFloat("05/10/2016")); //Output is 5
    </script>

 
    4. toFixed()

    Converts a number to a decimal value with specified number of digits after the decimal point.

  <script type="text/javascript">
        //toFixed()
        document.write((25).toFixed(2)); //Output is 25.00
        document.write((25.58).toFixed(2)); //Output is 25.58
        document.write((25.52).toFixed(3)); //Output is 25.520
        document.write((25.4578).toFixed(3)); //Output is 25.458
    </script>


    5. toString()

    Converts a number to a string.

<script type="text/javascript">
        //toString()
        document.write((25).toString()); //Output is 25
        document.write((25.58).toString()); //Output is 25.58
        document.write((25 + 50).toString()); //Output is 75
        document.write((25.52 + 24.64).toString()); //Output is 50.16
    </script>


    6. isFinite()

    Checks whether if a number is a finite number and returns true or false accordingly

 <script type="text/javascript">
        //isFinite()
        document.write(isFinite(25)); //Output is true
        document.write(isFinite("This is a string")); //Output is false
        document.write(isFinite("77")); //Output is true
        document.write(isFinite("05/10/2016")); //Output is false
    </script>

 
    7. toPrecision()

    Converts a number to a decimal number string with total digits (digits on left and right of decimal point) as specified.

  <script type="text/javascript">
        //toPrecision()
        document.write((2).toPrecision(2)); //Output is 2.0
        document.write((25.58).toPrecision(2)); //Output is 26
        document.write((25.52).toPrecision(3)); //Output is 25.5
        document.write((25.4578).toPrecision(4)); //Output is 25.46
    </script>


    Hope this will be helpful!

No comments:

Post a Comment