Discussion Locked

Javascript parseFloat Replacement

0 helpful
1 reply
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hello.

I am looking for information regarding a Javascript action that I only learned about yesterday.

Up to now, I have been using parseFloat to get data from a form. For example, I might have the following line:

tempx = parseFloat(document.forms[0].elements[xIndex].value);

However, somebody told me that a better string-to-number type conversion method is the unary plus operator (+). It is said to be about 4 times faster than parseFloat and still produces NaN results if the string cannot be converted into a number (for checking to ensure a valid number is input).

So my above line would become:

tempx = (+document.forms[0].elements[xIndex].value);

Can somebody please let me know more about this feature, direct me to some documentation, etc.

Also, when using EITHER of these functions to input numbers FROM A FORM, is it best practice to explicitly check that the input is valid? Or will both these functions return NaN for invalid input anyhow?

Any help would be appreciated.

Thank-you.

1 reply

by rivsys

Here is my advice:
1) Check all sources currently available to you. If your sources are inadequate, then obtain additional sources.
2) When you hit a wall, get advice from a trusted peer.
3) Repeat steps 1 and 2 until you have amassed enough information to statisfy your requirement.

I am always stunned by people like mathguy7 who are too lazy or otherwise incapable of conducting their own research, choosing rather to waste the time of others. These information leaches are productivity killers in any commercial setting, and are always found at the forefront of the disheartened heading out the door in a layoff. Following the above method will no doubt change mathguy7 from a productivity killing information leach to a valuable information resource his peers can rely on.

This discussion is closed to new replies.