428,839 Members | 2,270 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,839 IT Pros & Developers. It's quick & easy.

# is there a JS equivelent

 P: n/a of "int" in VB? //b=1.2 b=int(b) //b now equals 1 Sep 16 '06 #1
10 Replies

 P: n/a Dave Cox wrote: is there a JS equivelent of "int" in VB? var i = 1.2; window.alert( parseInt(i,10) ); // 1, fractional part is disregarded also: window.alert( Math.floor(i) ) // 1, integer smaller than 1.2 window.alert( Math.ceil(i) ) // 2, next integer bigger than 1.2 window.alert( 'i is ' + ((parseInt(i)==i) ? 'integer' : 'fractional') ) // test Sep 16 '06 #2

 P: n/a VK wrote: Dave Cox wrote: >is there a JS equivelent of "int" in VB? var i = 1.2; window.alert( parseInt(i,10) ); // 1, fractional part is disregarded also: window.alert( Math.floor(i) ) // 1, integer smaller than 1.2 window.alert( Math.ceil(i) ) // 2, next integer bigger than 1.2 Uhm. floor() and ceil() are nice functions, but neither of them are equivialent to int(). parseInt() is a better choice. -- Trond Michelsen Sep 16 '06 #3

 P: n/a Trond Michelsen wrote: floor() and ceil() are nice functions, but neither of them are equivialent to int(). parseInt() is a better choice. Sorry for possibly being ambiguous. Of course only parseInt(n,10) is pretty much what CInt() does in VB. I just wanted to add after that "(see) also (some useful methods for numbers treatment in relevance to integer/fractional question)". Sep 16 '06 #4

 P: n/a Trond Michelsen wrote: VK wrote: >Dave Cox wrote: >>is there a JS equivelent of "int" in VB? var i = 1.2;window.alert( parseInt(i,10) ); // 1, fractional part is disregardedalso:window.alert( Math.floor(i) ) // 1, integer smaller than 1.2window.alert( Math.ceil(i) ) // 2, next integer bigger than 1.2 Uhm. floor() and ceil() are nice functions, but neither of them are equivialent to int(). parseInt() is a better choice. Because it converts numeric arguments to strings and then parses those strings - parseInt - can produce very wrong results with numbers that would have exponented string representations. For example:- parseInt(0.00000025); - returns 2 when zero is really the nearest integer value. Its use in this role may introduce intermittent chaotic effects in scripts. Math.floor looks like the nearest native equivalent of VBScript's Int function. Richard. Sep 16 '06 #5

 P: n/a Richard Cornford wrote: Trond Michelsen wrote: >VK wrote: >>Dave Cox wrote:is there a JS equivelent of "int" in VB?var i = 1.2;window.alert( parseInt(i,10) ); // 1, fractional part is disregardedalso:window.alert( Math.floor(i) ) // 1, integer smaller than 1.2window.alert( Math.ceil(i) ) // 2, next integer bigger than 1.2 Uhm. floor() and ceil() are nice functions,but neither of them are equivialent to int().parseInt() is a better choice. Because it converts numeric arguments to strings and then parses those strings - parseInt - can produce very wrong results with numbers that would have exponented string representations. For example:- parseInt(0.00000025); - returns 2 when zero is really the nearest integer value. Its use in this role may introduce intermittent chaotic effects in scripts. Ah. I sorta suspected parseInt() could have some issues, but this one didn't occur to me. Thanks. Math.floor looks like the nearest native equivalent of VBScript's Int function. Yes, you're right. I thought Math.floor() would only be appropriate if it's a positive number. But, I should of course have checked how the Int function actually works in Visual Basic, instead of assuming it behaves like it does in other languages, like perl or python. -- Trond Michelsen Sep 16 '06 #6

 P: n/a Richard Cornford wrote: Because it converts numeric arguments to strings and then parses those strings - parseInt - can produce very wrong results with numbers that would have exponented string representations. For example:- parseInt(0.00000025); - returns 2 when zero is really the nearest integer value. Its use in this role may introduce intermittent chaotic effects in scripts. Math.floor looks like the nearest native equivalent of VBScript's Int function. var n = 9007199254740993.1; alert(Math.floor(n)); // 9007199254740994 // "intermittent chaotic effects" !! :-) The matter is not about some particular method, but about of how are numbers represented internally and about IEEE floating point numbers presicion limits. See a very informative thread "How are JS numbers represented internally?": Especially see the excellent summary by Lasse Reichstein Nielsen: (I constructed my "failure case" for Math.floor in one second by using this summary). In this aspect I see the job as not fully accomplished yet. That should be an abandonned practice to describe IEEE presicion issues as some kind of natural disaster a la earthquake ("A Big One can happen, see the photos", "It can error out, see the sample" :-) Instead it should be finally described in terms of IEEE precision limits with exact "secure borders" given and workarounds for beyond these borders. Sep 17 '06 #7

 P: n/a Thanks for your replies, help appreciated. Dave Sep 17 '06 #9

 P: n/a Trond Michelsen wrote: Richard Cornford wrote: >Because it converts numeric arguments to strings and thenparses those strings - parseInt - can produce very wrongresults with numbers that would have exponented stringrepresentations. For example:-parseInt(0.00000025);- returns 2 when zero is really the nearest integer value.Its use in this role may introduce intermittent chaoticeffects in scripts. Ah. I sorta suspected parseInt() could have some issues, but this one didn't occur to me. Thanks. Given the frequency of examples where - parseInt - is used in animation to 'round' the results of floating point calculations you have to think that this doesn't occur to many. Probably the aspect that is least appreciated is that - parseInt - must type-convert its numeric arguments to strings prior to parsing those strings. This is inevitably a relatively time consuming so would be rejected by the informed on those grounds alone, as animation really needs the calculation aspect of the problem to be as quick as possible if the presentation is to be smooth. If the range in which the results in known to be smaller than a signed 32 bit integer (as will always be the case in animation as screens do not yet get bigger than that) and truncation fractional parts is sufficient 'rounding', then operations that apply the internal - ToInt32 - function can be the optimum choice. These would be any bitwise operations (except >>-, which uses ToUint32) such a bitwiser OR and shift operations. For quick (4 to 14 times faster than all alternatives) truncating (towards zero) of integers where the range of the outcome could be accommodated by a signed 32 bit integer any bitwise operation that would not alter the value of an integer that was alrady in that range would do. These include OR zero and right or left shift of zero bits:- var int = (nFloat | 0); - or:- var int = (nFloat << 0); >Math.floor looks like the nearest native equivalent ofVBScript's Int function. Yes, you're right. I thought Math.floor() would only be appropriate if it's a positive number. But, I should of course have checked how the Int function actually works in Visual Basic, instead of assuming it behaves like it does in other languages, like perl or python. It is an interesting phenomenon that it never occurs to people who come here to ask about a javascript equivalent of some feature of some other programming language to state what it is that feature does in that other language. Even though it should be obvious that all programmers cannot know all languages and so at lest some will not know the language in question, but that people who know javascript will know what its features do and so will be able to match a specification with any corresponding native feature, or implement an equivalent if it is not natively available. Richard. Sep 17 '06 #10

 P: n/a JRS: In article , dated Sat, 16 Sep 2006 11:01:54 remote, seen in news:comp.lang.javascript, Dave Cox of "int" in VB? In News, the body of an article should stand independently of its subject line. Don't assume that other software behaves the same as yours. The practical answer for such questions can depend on the range of argument needed. If the argument can never be negative nor very large, you are likely to have a wider choice than if you must match a wider range. Generally, to get a reliable translation from VB to JS, or between any other pair of natural or artificial languages, one needs a reasonable understanding of what is happening in the source language to be sure of making a sound re-implementation. It's a good idea to read the newsgroup and its FAQ. -- © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 © ? JL/RC: FAQ of news:comp.lang.javascript

### This discussion thread is closed

Replies have been disabled for this discussion.