Book Section
-----
TY JavaScript 3rd Ed.
Teach Yourself JS 1.5
Teach Yourself DHTML
Teach Yourself JS 1.3
LLWW: JavaScript

General Section
-----
Discussion Forum
Articles / Tips
JavaScript Links
About the Author
Privacy Policy
Contact Me



Other Sites
-----
Website Workshop
JavaScript Weblog

JavaScript Workshop Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Hints on Numbers vs Strings - (Number/String conversion tric

 
Post new topic   Reply to topic    JSWorkshop Forum Index -> Articles
View previous topic :: View next topic  
Author Message
phil karras
Senior Member
Senior Member


Joined: 15 Jul 2002
Posts: 1750
Location: MD

PostPosted: Tue Jul 16, 2002 7:06 am    Post subject: Hints on Numbers vs Strings - (Number/String conversion tric Reply with quote

Idea
Numbers vs strings: A number of people have asked how to insure that their numbers are treated as numbers, an example here is:

a = "5"; // Here I force a & b to be strings to start with, just like data entry(inputs)
b = "6";
value = a + b;
window.alert(value); // prints out 56 why?

JavaScript considers everything a string first and foremost, and since "+" is defined as a string operator (concatenation) it does that if it can. In the above case we did not tell it differently so the string operation works.

The normal way to force the issue is to use parseInt() as in:
value = parseInt(a) + parseInt(b);

However, I've found that there are a few simple ways to force the inputs to be numbers, example:

a = "5"; // Here I force a & b to be strings to start with, just like data entry(inputs)
b = "6";
value = a/1 + b*1;
window.alert(value); // prints out 11 as I want

If a user enters a "number" it is always considered to be a string, here are a few things that will force your numbers to be considered and operated on as numbers:
++a; // iterate it by 1 (changes the value)
--a; // decrement it by 1 (changes the value)
a/1; // divide it by 1 (does NOT change the value)
a*1; // multiply it by 1 (does NOT change the value)
(a-0); // subtract 0 (does NOT change the value)
parseInt(a); // returns a number (does NOT change the value)

BUT DON'T USE: (a+0) because that once again allows it to remain a string! (In this case "50".)

In one case I have a rather long equation that used inputs from someone and sure enough the numbers were not coming out correctly. I would have had to type "parseInt" about four times so I tried value/1 and simply "/1" divided the four numbers by 1 and sure enough everything worked correctly.

This is because while JS considers an input to be a string, if it is a number, it will "convert" it to a number when you perform a numeric operation on it, such as dividing by 1.

So, when in doubt, try the simple method of dividing or multiplying by 1 to force JS to consider your input as a number.

Question:
value = a-0 + b-0;
window.alert(value); // prints out 56

BUT
value = (a-0) + (b-0);

window.alert(value); // prints out 11

why?

One would think that since "-" is a math only operation (there being NO corresponding string operator "-") that this would "force" our values a & b to be numbers. Well, sort of, here’s what happens:

JavaScript comes along and reads (parses) this equation from left to right so it first sees a - 0, so it makes a into a number so it can operate on it, then it sees + b but b is a string and "+" is also a string operator and string operations take precedence, so it concatenates "6" onto the number 5, but to do that JS must first re-convert a back into a string, which it gladly does, and we end up with 56, then JS see - 0 again and converts the string 56 into a numeric 56 and adds 0 to it.

To prove this is in fact the case, add a 1 to it like this:
value = a-0 + b-0;
window.alert(value); // prints out 56

value = value + 1;
window.alert(value); // prints out 57 NOT 561 !

On the other hand try:
value = a-0 + b;
window.alert(value); // prints out 56

value = value + 1;
window.alert(value); // prints out 561 NOT 57!

Thus we have proved that the last - 0 converted the string 56 into a numeric 56, without it we got 561 and with it we got 57.

I hope this helps.
_________________
Phil K
Circle Software Consulting
Test website: http://cs.yrex.com/
Guidelines for Posting: http://jsworkshop.com/posting.html
IHBAAA = It Has Been Asked And Answered
KISS: http://jsworkshop.com/bb/viewtopic.php?t=508


Last edited by phil karras on Tue Nov 08, 2011 4:54 am; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
phil karras
Senior Member
Senior Member


Joined: 15 Jul 2002
Posts: 1750
Location: MD

PostPosted: Wed May 21, 2003 9:18 am    Post subject: Reply with quote

Looks like I got it all corrected, only took 6 years?
_________________
Phil K
Circle Software Consulting
Test website: http://cs.yrex.com/
Guidelines for Posting: http://jsworkshop.com/posting.html
IHBAAA = It Has Been Asked And Answered
KISS: http://jsworkshop.com/bb/viewtopic.php?t=508
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    JSWorkshop Forum Index -> Articles All times are GMT - 7 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2002 phpBB Group
(c) 1997-2002 Starling Technologies and Michael Moncur. Portions (c) Sams Publishing.