Wrox Programmer Forums Strange string concat
 |
 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

September 3rd, 2005, 01:47 PM
 edislasm Registered User Join Date: Jul 2003 Posts: 7 Thanks: 0 Thanked 0 Times in 0 Posts
Strange string concat

Hi, this seems to be stupid but i find no explanation. I have this lines for testing the problem:

the form can accept empty values and it seems that parseInt('') = NaN so, as I would do in VBscript or in Java, could fix the problem like this:

but it seems that a zero on the left does real diference!. For example, for a value in Cantidad2 of '1', I can put any value from '0' to '7' and even empty string. But after number '8' I don't really get the sum of Cantidad1 + 1. This is the arithmetics that is doing for example:

'' + 1 = 1
'4' + '1' = 5
'8' + '1' = 1
'200' + '1' = 129

Strange huh?... and how to fix this??

September 5th, 2005, 01:03 PM
 philip_cole Friend of Wrox Join Date: Sep 2005 Posts: 166 Thanks: 2 Thanked 33 Times in 33 Posts

hmmm interesting. I played around, and it looks like it treats the value as octal (base 8). This means:
0-7 is same,
8 and 9 get ignored (are parsed as zero),
200 is 8*8*2 +0+0 = 128.

I would get round this by checking if the values are empty first:

.. or ask them to enter the numbers in octal :)

Philip Cole

September 6th, 2005, 02:17 AM
 joefawcett Wrox Author Join Date: Jun 2003 Posts: 3,074 Thanks: 1 Thanked 38 Times in 37 Posts

As Philip has pointed out parseInt treats sequences starting with 0 as octal. Either specify the base you are converting to:
Code:
`parseInt(Cantidad, 10);`
or don't use it all, I'm not sure why parseInt is always used in examples, use
Code:
` Math.floor(Cantidad);`
--

Joe (Microsoft MVP - XML)

 Similar Threads Thread Thread Starter Forum Replies Last Post How to do Very Large String Concat? EcceBozo XSLT 2 July 29th, 2008 03:58 PM String.Concat NOT same as '&" Ron Howerton Visual Basic 2005 Basics 0 November 21st, 2006 04:34 PM Mailto string and concat paul20091968 Access VBA 1 August 10th, 2006 12:40 PM concat function help! migake XSLT 2 May 2nd, 2006 09:31 AM concat problems gonesail XSLT 1 May 12th, 2004 08:19 PM