Search
Left Quote    Choose a job you love, and you will never have to work a day in your life.
- Confucius    
Right Quote
 
[login] | [Register]
 

Learning JavaScript Basics

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

CONVERTING A NUMBER TO A STRING

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:

CONVERING STRINGS TO NUMBERS



Ok, working opposite now. What if we have a string, and want to make it a number? 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 right? Yup, so we will do that by using this method below in the code example:


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

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




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


No Comments for this page.

You Must be logged in or a member to comment.


Tutorial Stats

Tutorial Stats

82,590 Views
1 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    

Your Article Here


Flights - Car Insurance - Renegade Motorhomes - Loans

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