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(). On Feb 24, 9:47 pm, Marcel Laverdet <[email protected]> wrote: > So? That's nothing we haven't covered here. Personally I'd recommend: > String.prototype.toNumber = function() { return Number(this) }; > > Actually I wouldn't recommend it, I'd recommend just using Number() or + > without touching the prototype. > > > > > > > > On Fri, Feb 24, 2012 at 1:46 PM, Jorge <[email protected]> wrote: > > On Feb 24, 2012, at 8:37 PM, Marcel Laverdet wrote: > > > > 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. > > > -- > > 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 [email protected] > > To unsubscribe from this group, send email to > > [email protected] > > For more options, visit this group at > >http://groups.google.com/group/nodejs?hl=en?hl=en -- 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 [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en
