On Feb 27, 2012, at 03:25, Lothar Pfeiler wrote: > 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(). -- 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 nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en