Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-23-2004, 05:30 PM   PM User | #16
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
eval is definitely not more efficient. Quite the opposite, actually. When you take Function.prototype.apply into count, there's very few uses of eval where there is not a better, more efficient solution.

Well, let's give you a feature comparison:
  • Numerical operators (unary positive +; unary negative -; binary subtraction -; binary multiplicative *, /, %):
    Gives a floating point result for an entire string. E.g. "08"-0 returns the number 8; +"1e6" returns the number 1000000; while 1*'1d6' returns the number NaN.
  • Using the Number function:
    Gives the same results as the numerical operators, but might be easier to understand when they occur in the code.
  • Using the Number constructor:
    Gives a number object instead of a number primitive, but in all other aspects behaves like the Number function.
  • Using the parseFloat function:
    Gives a floating point number for characters in the string up to the point where they may no longer be interpreted as floating point numbers. E.g. parseFloat("08") returns the number 8; parseFloat("1e6") returns the number 1000000; parseFloat("1d6") returns the number 1; parseFloat("0x1d6") returns the number 0 (Inconsistent, op7.5 returns 470); and parseFloat("d") returns the number NaN.
  • Using the parseInt function:
    Gives an integer number for characters in the string up to the point where they may no longer be interpreted as integer numbers. It also contains the same octal and hexadecimal autodetection system as the eval function and JavaScript numeric literals do. parseInt("1e6") returns the number 1; parseInt("08") returns the number 0; parseInt("0x1d6") returns the number 470; and parseInt("d") returns the number NaN
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards
liorean is offline   Reply With Quote
Old 05-23-2004, 05:40 PM   PM User | #17
Tails
Regular Coder

 
Join Date: Nov 2002
Posts: 672
Thanks: 1
Thanked 1 Time in 1 Post
Tails is an unknown quantity at this point
Quote:
Originally Posted by Willy Duitt
You can also convert a string into a number by multiplying by one.
var R=document.formN.ElementN.value*1;
I wouldn't rely on that. You might get a NaN error (Not a number).
Tails is offline   Reply With Quote
Old 05-23-2004, 05:42 PM   PM User | #18
Basscyst
Smokes a Lot


 
Join Date: Jul 2003
Location: CA, USA
Posts: 1,594
Thanks: 5
Thanked 20 Times in 20 Posts
Basscyst is on a distinguished road
Way to break it down. Excellent. Thank you professor Liorean. Is that in the FAQ? It should be.

Basscyst
__________________
Helping to build a bigger box. - Adam Matthews
Basscyst is offline   Reply With Quote
Old 05-23-2004, 05:46 PM   PM User | #19
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
Tails: You may get NaN as a result from all ways of converting a string to a number, as you can see from my examples above. You can also get different interpretations of the number entered depending on the code you use. If you really want to assure yourself it's a number, you should use a regular expression to detect anything you don't want to allow in the string, and then use the isNaN function to test the result anyway.

Edit:
Quote:
Originally Posted by Basscyst
Way to break it down. Excellent. Thank you professor Liorean. Is that in the FAQ? It should be.
It's not in the FAQ, though question 7 is related. I think it might belong in there, if one formats it better. (a table-like formatting with each of the test strings, and the results of applying each of the methods on it, perhaps?)

I've tried to cut down on my posting in them in an effort to try to get others to post there, so instead of adding it myself I invite you to adapt it to the FAQ form and post it there. Something that goes for all other similar cases of FAQ-worthy replies, of course.
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

Last edited by liorean; 05-23-2004 at 06:03 PM..
liorean is offline   Reply With Quote
Old 05-23-2004, 06:01 PM   PM User | #20
Willy Duitt
Banned

 
Join Date: Sep 2003
Posts: 3,620
Thanks: 0
Thanked 0 Times in 0 Posts
Willy Duitt is an unknown quantity at this point
Quote:
Originally Posted by Tails
I wouldn't rely on that. You might get a NaN error (Not a number).
I do it all the time. But of course you have to be working with a number to start with. If you have any doubt that the string value will contain anything other than a numeric and you want a number, you should be validating anyway.

Sorry Liorean; I did not see your last post before responding.
Willy Duitt is offline   Reply With Quote
Old 05-23-2004, 06:51 PM   PM User | #21
Basscyst
Smokes a Lot


 
Join Date: Jul 2003
Location: CA, USA
Posts: 1,594
Thanks: 5
Thanked 20 Times in 20 Posts
Basscyst is on a distinguished road
Quote:
I've tried to cut down on my posting in them in an effort to try to get others to post there, so instead of adding it myself I invite you to adapt it to the FAQ form and post it there. Something that goes for all other similar cases of FAQ-worthy replies, of course.[/
OK, will do. At work right now though, will put it in there this evening.

Basscyst
__________________
Helping to build a bigger box. - Adam Matthews
Basscyst is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:56 PM.


Advertisement
Log in to turn off these ads.