Wrox Programmer Forums
| 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 September 3rd, 2005, 01:47 PM
Registered User
 
Join Date: Jul 2003
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to edislasm
Default Strange string concat

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

var cantidad;
cantidad = 0;
cantidad = cantidad + parseInt(document.forms[0].Cantidad.value);
cantidad = cantidad + parseInt(document.forms[0].Cantidad2.value);

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:

var cantidad;
cantidad = 0;
cantidad = cantidad + parseInt('0' + document.forms[0].Cantidad.value);
cantidad = cantidad + parseInt('0' + document.forms[0].Cantidad2.value);
alert(cantidad);

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??



 
Old September 5th, 2005, 01:03 PM
Friend of Wrox
Points: 894, Level: 11
Points: 894, Level: 11 Points: 894, Level: 11 Points: 894, Level: 11
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: London, , United Kingdom.
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

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:

var cantidad;
cantidad = 0;
if(document.forms[0].Cantidad.value.length > 0)
  cantidad = cantidad + parseInt(document.forms[0].Cantidad.value);
if(document.forms[0].Cantidad2.value.length > 0)
  cantidad = cantidad + parseInt(document.forms[0].Cantidad2.value);
alert(cantidad);

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

Philip Cole
 
Old September 6th, 2005, 02:17 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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





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