473,413 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

When is something a number and not a number

Claus Mygind
571 512MB
Using the following coding

Expand|Select|Wrap|Line Numbers
  1.  
  2. <input type text id="myField" value ="" />
  3.  
  4. var cThisVal = parseFloat( document.getElementById("myField").value)
  5.  
  6.  
When I check the value of "cThisVal" and see the response is NaN not a number.

I then test to see if it a number like this.

Expand|Select|Wrap|Line Numbers
  1. typeof cThisVal == "number"  evaluates to false
  2. typeof document.getElementById("myField").value == "number"  evaluates to true
So am I to assume that "parsefloat" makes cThisValue a number even though it is not. Also if it evaluated as a number, what is the value?
Sep 30 '08 #1
2 1968
Claus Mygind
571 512MB
Ok I think I got that one figured out. just use the "isNaN(cThisVal)" function instead to get the right result.
Sep 30 '08 #2
gits
5,390 Expert Mod 4TB
yep ... in such cases the isNaN() method could/should be used. for parseFloat() it is correct that this method will cast the string that is given to it to a js-floating number ... and when it cannot be parsed correctly then the method returns NaN (not a number) ... but we cannot really rely on that ... have a look at the following examples:

Expand|Select|Wrap|Line Numbers
  1. var s = '1.foo';
  2. alert(parseFloat(s)); // alerts 1
  3.  
  4. var s1 = '.1s';
  5. alert(parseFloat(s1)); // alerts 0.1
so even when the value is not a number parseFloat() does the 'best' to parse it and returns the number that could be parsed by just leaving out the characters that invalidate the number ... so we would have (at least we could have) a logical error when we would rely on parseFloat()'s return to decide the value was a number or not :) because both of them in the example where not ... but in both of the cases we don't get a NaN ...

kind regards
Sep 30 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: ewitkop90 | last post by:
Here is my code: <SCRIPT> function transportchange(transport) { if (framenewinstall.Helpdesk.checked) framenewinstall.Helpdesk.checked=false; if (framenewinstall.CircuitNumber.checked)...
7
by: Peng Yu | last post by:
I'm not very clear about when to use #define and when to use const? #define number 4 const int number = 4; If I just want to define a global constant, which way of the above is better? ...
2
by: Serge | last post by:
Hi, I am having a thread hang problem in my c# code. The example on the website: http://csharp.web1000.com/ is a simplified version of my problem. You will see in the form that a...
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
11
by: bill | last post by:
I dynamically create buttons and associate them with an event using AddHandler. I want all the button events to fire at one time, when the page is posted, instead of when each button is clicked....
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
10
by: Lisa | last post by:
In translating the formula for calculating lottery odds for various conditions into a Visual Basic Program, I have apparently missed something in that I get errors in the part of the calculation...
94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
44
by: Smokey Grindle | last post by:
I have a list box on my form, but I need to databind it to a data table that is a private member of the form's class... so I basically have Public Class MyForm priate m_MyTable as new datatable...
0
by: raylopez99 | last post by:
I ran afoul of this Compiler error CS1612 recently, when trying to modify a Point, which I had made have a property. It's pointless to do this (initially it will compile, but you'll run into...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.