Results 1 to 6 of 6

Thread: Coversion of Cast string as int in Javascript

  1. #1
    Join Date
    Jan 2010
    Posts
    51

    Coversion of Cast string as int in Javascript

    How many ways to perform a conversion of string value into integer values.Can you tell me about the ParseInt function and what is role of it. I need a project that perform a calculation on string .

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Coversion of Cast string as int in Javascript

    A string into Integer

    A string is a collection of character, and if we have desire to convert it into Integer for some calculations then we can do it very easily using provided functions in java that come across into Java script .

    A program that evaluate a string into integer value:


    <script>

    function covertint(o)
    {
    var strval = "023";
    var result = parseInt(strval);
    console.log(result);
    }

    </script>
    Last edited by Praetor; 19-01-2010 at 11:45 AM.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Coversion of Cast string as int in Javascript

    There are many ways to covert a string value into integer if we look back in the previous study of java script.You can achieve it using :

    - Using typeof keyword
    - Using parseInt Function

    Using typeof Operator:

    <script>
    function usetypeof()
    {
    var charval= "52";
    var mtier = +charval;
    console.log(typeof mtier);
    }
    </script>
    Output would be :52 (in Number)

    Using ParseInt Function:

    <script>
    function useparseint()
    {
    var string = "021";
    var numbr = parseInt(string);
    console.log(numbr);
    }
    </script>

    Output would be : 21

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Coversion of Cast string as int in Javascript

    Conversion of String to Integer in Java:

    A program that accepts input as a string value and produce output after converting it into integer.


    public class convert{

    public static void main(String[] args)
    {

    String stringval = new String("10");//declaration of string
    int mediavalue = Integer.parseInt(stringval);
    System.out.println(mediavalue);

    }

    }

  5. #5
    Join Date
    Oct 2010
    Posts
    1

    star convert number to string

    how check first 4 numbers of the mobile number.for example 9382234567
    first check the first 4 numbers for 9382 .in this numbers entered in the text box after that remaining numbers are allowing otherwise it returns false message.how can coded using java script..plz reply

  6. #6
    Join Date
    Feb 2010
    Posts
    137

    Re: Coversion of Cast string as int in Javascript

    You will need to use an authentication in the coding so that mobile number starting from 9382 (example) will only be accepted. You can use JavaScript's Regular Expression Methods for this purpose. The standard expression method in JavaScript has two key methods for testing strings: test() and exec().
    • The exec() Method
      The exec() method takes one argument, a string, and checks whether that string encloses one or more matches of the pattern individual by the regular expression. If one or more matches are found, the method returns a consequence array with the starting points of the matches. If no match is found, the method returns null.
    • The test() Method
      The test() method also takes one argument, a string, and checks whether that string contains a go with of the pattern specified by the regular expression. It returns true if it does surround a match and false if it does not. This method is very helpful in form validation scripts. The code sample below shows how it can be used for checking a social inspecting number.

    Code:
    <html>
    <head>
    <title>Social Security Number Checker</title>
    <script type="text/javascript">
    var AB_SSN = /^[0-9]{3}[\- ]?[0-9]{2}[\- ]?[0-9]{4}$/;
    
    function checkSsn(ssn){
     if (AB_SSN.test(ssn)) {
      alert("VALID SSN");
     } else {
      alert("INVALID SSN");
     }
    }
    </script>
    </head>
    <body>
     <form onsubmit="return false;">
      <input type="text" name="ssn" size="15">
      <input type="button" value="Check" 
       onclick="checkSsn(this.form.ssn.value);">
     </form>
    </body>
    </html>

Similar Threads

  1. Find and Replace in JavaScript String
    By Wilbur in forum Software Development
    Replies: 5
    Last Post: 15-12-2009, 02:23 PM
  2. JavaScript to convert an Array to String
    By KAMANA in forum Software Development
    Replies: 3
    Last Post: 09-12-2009, 05:30 AM
  3. Trim a String in JavaScript
    By Kingfisher in forum Software Development
    Replies: 3
    Last Post: 21-11-2009, 10:32 AM
  4. How to cast float value into integer in javascript
    By Antarjot in forum Software Development
    Replies: 3
    Last Post: 21-11-2009, 08:58 AM
  5. Get String Length with Javascript
    By klite in forum Software Development
    Replies: 3
    Last Post: 31-10-2009, 11:39 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,197,176.22496 seconds with 17 queries