Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
| Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 17th, 2003, 04:59 PM
Registered User
 
Join Date: Oct 2003
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Converting a string to a number

How do you convert a string to a number in JavaScript?

 
Old November 17th, 2003, 05:15 PM
Registered User
 
Join Date: Nov 2003
Location: Worthington, OH, USA.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Easiest way is to use eval()
<script>
    var test='1';
    var t2=2;
    alert(eval(test)+t2);
</script>

Returns 3, whereas

<script>
    var test='1';
    var t2=2;
    alert(test+t2);
</script>

Returns 12
 
Old November 17th, 2003, 06:01 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You could also use

string.parseInt()


Peter
------------------------------------------------------
Work smarter, not harder.




Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting number to hh:mm dlyles Beginning VB 6 8 September 19th, 2006 12:27 PM
Converting non 24hr time into number joehoe VB.NET 2002/2003 Basics 1 June 9th, 2005 08:54 AM
how to replace a string with another string/number crmpicco Javascript How-To 4 March 14th, 2005 12:59 PM
Converting String to an integer Louisa VB.NET 2002/2003 Basics 2 March 5th, 2004 10:25 AM
Converting int to string Judex C++ Programming 3 March 1st, 2004 10:44 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.