Search
Left Quote    Wise men speak because they have something to say; Fools because they have to say something.
- Plato (427-347 B.C.)    
Right Quote
 
[login] | [Register]
 

Learning JavaScript Basics

by: bs0d
Page: 3 of 10
(View All)

CONVERTING A STRING TO A NUMBER

Here is a quick question. What do you think will be the typeof the following variable:
num1= "45";


If you said, "number" that is incorrect. Yes, it is a number and eventhough the variable name is "num1" that does not matter. It could be named anything. What matters is that the values are enclosed in double quotes. So, automatically JavaScript is recognizing the variable as a string. So if you answered "string" then you are on top of this!

parseInt();



But what if you wanted that variable to be recognized as a number instead of a string? Well, that is where parseInt(); function comes to play. So before you can perform arithmetic operations on a group of numbers, you need to know you're dealing with numbers!

parseInt's duty is to convert a string into an integer. It starts this process from the left hand side, and begins converting digits until it runs into a non numerical digit. From this point, the process will stop. Whatever has been converted will automatically be assigned to the variable. If the first character encountered is not a numerical value, then parseInt will display "NaN" - which stands, for not a number.

Now a practice use for parseInt would be if you are using numerical values obtained from user input. So, if they enter, "stupid" instead of a #, you can test with parseInt if it is indeed a number, and if not, re-prompt them to enter a value.

Here is an example of how parseInt(); will work:




As you might see in var1 and var2, parseInt stopped after "7" in var1 because of the "U", and in var2, it stopped after the "7" because of the "U". In var3, it saw the "W" and saw it was not a number (NaN). For var4, it stopped after the number "3" because of the "A", even though after "ABC" numbers pick back up- parseInt stops after the first non numeric digit, not all digits within the variable. var5 stopped when it saw the "." because it is a non numeric digit as well.

Though with parseInt(); JavaScript will not recognize letters, if you do use a minus sign (-), the string will be converted to a negative number. The same concept applies to the plus sign (+) where it will be considered a positive number. BUT, if the "+" or "-" sign appears anywhere else that then first character, they will be considered a non numeric character.

parseFloat();



What you needed to recognize floating point numbers? Well, this is where the parseFloat(); function comes into action. parseFloat works exactly like parseInt, except it works with floating point numbers instead of just integers. So when parseFloat comes to a period "." it will interpret it as a decimal point (where parseInt saw it as a non numeric digit). However, if a second period is noticed, parseFloat will see it as a non numeric digit.

Examples:

CONVERTING NUMBERS TO STRINGS



Ok, working opposite now. What if we have a number, and want to make it a string? Well, we are lucky that JavaSript has the toString(); function! How you will use this, is by simply adding the toString(); function at the end of your variable name seperating the variable and function with a period.

Example:



There is also another way to turn a number into a string, and that is by adding double quotes to it. We will do that by using the method outlined within the code in the example below:


There you have it. Now you know how to convert a number to a string.

Here is a sample JavaScript program using the information we covered:




1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |  10  |  
Next »


Comments:

  sstrong
  Subject: "CONVERING STRINGS TO NUMBERS" Date: Feb 20 2009 at 11:50 pm    
under this heading, which I assume should be "CONVERTI NG STRINGS TO NUMBERS" you state "Ok, working opposite now. What if we have a string, and want to make it a number?" but the illustration shows code to take a number, and make it a string, correct? I'm not trying to be picky, I'm just trying to make sure I understand.
  bs0d
  Subject: "Correction" Date: Feb 22 2009 at 8:06 pm    

Yes, it appears as though there was a typo and some transposed verbage. Correction made, thanks.
You Must be logged in or a member to comment.


Tutorial Stats

Tutorial Stats

391,501 Views
3 Total Comments
4.66 Rating of 5 (3 Votes)

Options

Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Del.icio.us: Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article

Articles

Related    

· Getting Started with Ajax
Your Article Here



"AllSyntax.com" Copyright © 2002-2018; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank