--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
On a slightly different note, another good way (which Crockford also
suggests) is instead of using parseInt(), coerce the string into
number by using +'08'. This will give you the number 8.
Note that in your case this may not be what you want since it gives a
number, not an integer. This is fine if you know your string contains
a round number, but not if your string can contain a decimal point.
e.g. +'08.5' will give you the number 8.5.
If you ever use this in a calculation of some sort, it's also good to
put parens around your coerce. For example:
var x = something + (+'08') + whatever;
Cheers,
Andy
--
Andrew Chilton
e: chi...@appsattic.com
w: http://www.appsattic.com/
> I just use Number()Doesn't + do the same thing? I'm a lazy typist.
I use pluses without a space so it is quite readable. +a means parseInt. + a means there is concatenation or addition going on somewhere. It is quite intuitive.
On Tue, Feb 21, 2012 at 5:56 PM, Marcel Laverdet <mar...@laverdet.com> wrote:
Yeah it's identical to prefix +, just more "english" and doesn't blend in with string concatenation. It's totally a matter of preference. I personally find that while writing software I'm rarely blocked by raw typing speed so I use Number() because it makes +'s less ambiguous when I'm reading my code later.
On Tue, Feb 21, 2012 at 7:46 PM, Mark Hahn <ma...@hahnca.com> wrote:
> I just use Number()Doesn't + do the same thing? I'm a lazy typist.
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
+'123 px'
NaN
parseInt('123 px', 10)
123
--
Jorge.
~~"12345678901"
-539222987
--
Jorge.
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
> You could extend String.prototype if you wanted to but it's probably one of the less common ways to do this kind of thing. It has disadvantages like portability, and it will ONLY works on strings. Using Number() or + on something that's not a string (undefined, an object, a number) will work fine, but your prototype idea will fail. If you did want to go this way I'd recommend calling it `toNumber` to match the `toString` convention.
String.prototype.toInt= function toInt () { return parseInt(this, 10) };
"123 px".toInt()
123
--
Jorge.
> Maybe I don't get it, but how can I force Number() to give me an
> Integer? parseInt('2.3') will give me an integer. Number('2.3') will
> give me a float.
>
> Once, I gave my students the task to calculate the quarter of the
> year. With a type strict compiler language like C the algorithm is
> pretty simple: date.getMonth() / 3 + 1.
> Because they were not thinking of integers it was tough for them to
> figure out the algorithm. Sometimes you just need integers.
> parseInt(date.getMonth() / 3, 10) + 1.
>
> Plus, I thought this is a discussion about parseInt().
This discussion was initially a bug report about parseInt(), and quickly became an explanation of why it's not a bug and is instead behaving as designed, though clearly not as many people anticipate; this may mean it was not designed well.
By what algorithm would you like to convert a floating point number to an integer? There are several methods available to you, including Math.round(), Math.floor() and Math.ceil().
parseInt() is about converting a string representing an integer, in any of several formats including hex, octal and decimal, to a JavaScript integer; date.getMonth() / 3 is not a string and furthermore does not represent an integer so to me there's no reason to use parseInt() on it; just run the math function on it that you intend. For example if you mean to discard anything after the decimal point, maybe you want Math.floor().