Re: I have the answer! (was: Re: adding two quantities)
JRS: In article <bo1evo$153r1a$ 1@ID-174912.news.uni-berlin.de>, seen in
news:comp.lang. javascript, Fabian <lajzar@hotmail .com> posted at Sun, 2
Nov 2003 08:16:52 :-[color=blue]
>Douglas Crockford hu kiteb:
>[color=green]
>> You probably meant to write
>>
>> var sum = eval(s1 + " + " + s2);
>>
>> which is less wrong than your suggestion, but which does two
>> concatenations before invoking the compiler. This is a very bad way
>> to perform addition on two strings.[/color]
>
>I just checked my manual. Apparently, the following should work:
>
>var sum = parseFloat(s1) + parseFloat(s2);
>
>Im just a disinterested observer though. I just tested it and it seems
>to work too. Note the capitalisation is important.[/color]
One should not choose a method merely on the basis that it seems to
work. That condition is necessary but not sufficient.
If there is any prospect of the string having been supplied by an
unreliable system (including a user) without subsequent full format
verification, one should consider what happens with invalid strings.
Different methods give different results.
F.X0.value = "6.7 tons"
Q = [parseFloat(F.X0 .value), +F.X0.value] // result : 6.7,NaN
"3,14159" gives 3,NaN and three is clearly not what was hoped for;
yet it will give similar numerical results later.
"0XIDE" gives 0,NaN. "0XE" gives 0,14.
And
F.X0.value = "0077"
Q = [parseInt(F.X0.v alue), +F.X0.value] // result : 63,77
--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
JRS: In article <bo1evo$153r1a$ 1@ID-174912.news.uni-berlin.de>, seen in
news:comp.lang. javascript, Fabian <lajzar@hotmail .com> posted at Sun, 2
Nov 2003 08:16:52 :-[color=blue]
>Douglas Crockford hu kiteb:
>[color=green]
>> You probably meant to write
>>
>> var sum = eval(s1 + " + " + s2);
>>
>> which is less wrong than your suggestion, but which does two
>> concatenations before invoking the compiler. This is a very bad way
>> to perform addition on two strings.[/color]
>
>I just checked my manual. Apparently, the following should work:
>
>var sum = parseFloat(s1) + parseFloat(s2);
>
>Im just a disinterested observer though. I just tested it and it seems
>to work too. Note the capitalisation is important.[/color]
One should not choose a method merely on the basis that it seems to
work. That condition is necessary but not sufficient.
If there is any prospect of the string having been supplied by an
unreliable system (including a user) without subsequent full format
verification, one should consider what happens with invalid strings.
Different methods give different results.
F.X0.value = "6.7 tons"
Q = [parseFloat(F.X0 .value), +F.X0.value] // result : 6.7,NaN
"3,14159" gives 3,NaN and three is clearly not what was hoped for;
yet it will give similar numerical results later.
"0XIDE" gives 0,NaN. "0XE" gives 0,14.
And
F.X0.value = "0077"
Q = [parseInt(F.X0.v alue), +F.X0.value] // result : 63,77
--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Comment