473,471 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

is there a JS equivelent

of "int" in VB?

//b=1.2
b=int(b)
//b now equals 1
Sep 16 '06 #1
10 1819
VK

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
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
VK

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
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 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.
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
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 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.
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
VK

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?":
<http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/38d21acb4d4509ce>

Especially see the excellent summary by Lasse Reichstein Nielsen:
<http://groups.google.com/group/comp.lang.javascript/msg/3833df1762d81fee>
(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
VK wrote:
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
alert(n);

- also shows 9007199254740994, so the Math.floor method is not altering
a number that is already an integer when it is passed in.
// "intermittent chaotic effects" !! :-)
A discrepancy of 1 in 9007199254740994 is hardly going to produce
results nearly as chaotic as a discrepancy of 20+ orders of magnitude,
as - parseInt - produces.

var n = 1000000000000000000000;
alert(parseInt(n));

- alerts 1, not even close to the original value.
The matter is not about some particular method,
What is up, embarrassed by giving obviously bad advice again? You should
expect that by now as not knowing what you are doing will result in
that.
but about of how are numbers represented internally
and about IEEE floating point numbers presicion limits.
Beyond a certain point (two to the power of 53) javascript numbers
cannot represent all integers, and so specific integers can only be
represented by the closes approximation. Those approximations are small
in comparison to the magnitude of the numbers and anyone trying to work
with numbers of that size will encounter precision related issues
whatever they do.

What happens around zero is much more significant. Zero is more likely
to be in the range that is of interest and with all floating point
numbers being held as their nearest representable form addition and
subtraction of fractional values that may have been expected to produce
results that are zero may actually have produced results that are just
very close to zero. The - parseInt - fiction will produce very wrong
results in this significant area.

So using - parseInt - in, for example, an animation; an element may
approach a point smoothly, jump away by up to 9 pixels when it should be
at that point, and then smoothly carry on once it has passed that point,
and it may only do that when a set of variable (such as browser size)
have very particular values. An intermittently chaotic outcome that
would be difficult to spot and so an easy mistake to make for someone
who does not fully appreciate the interaction of the mechanism of -
parseInt - with the nature of the javascript number type.
See a very informative thread "How are JS numbers
represented internally?":
<snip>

When you say "informative", and baring in mind that you read that some
time ago (plus the many other threads trying to separate you from your
delusions about the representations of numbers on computer systems in
general and in javascript in particular), are you implying that you were
informed? And if so why "Of course only parseInt(n,10) is pretty much
what CInt() does in VB"? (Which combines not being true itself, not
being the question asked and not being a good answer to the question
asked)

Richard.
Sep 17 '06 #8
Thanks for your replies, help appreciated.

Dave
Sep 17 '06 #9
Trond Michelsen wrote:
Richard Cornford wrote:
<snip>
>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.
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 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.
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
JRS: In article <CQ*****************@newsfe7-win.ntli.net>, dated Sat,
16 Sep 2006 11:01:54 remote, seen in news:comp.lang.javascript, Dave Cox
<da*****@tiscalinotospam.co.ukposted :
>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 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 18 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Pieter | last post by:
Hi, I'm adding hundreds of strings to a listbox and I want the last added string to be visable to the user. In C++ there is a function SetTopIndex which does this, how do you do it in VB? ...
1
by: Lou | last post by:
If I need to create a .Net component to be run inside another Non .Net application, what kind of componenet do I create in .NET Example, I want to create a .Net component to be hosted in VB6 or...
2
by: John Baker | last post by:
Hi: I have a Pop Up form that involves a main form and sub form. The sub form has the results of a query on the main form. I need to automatically perform exactly he same action that takes...
7
by: Poewood | last post by:
I store all my global functions in modules when using vb. What is the equivelent convention in C#. I am converting a .net compact frmwk project from vb to C#. Thanx, Poe
8
by: bryan | last post by:
Is there any way I can get the application path (the one returned by Request.ApplicationPath) in the Application_Start method in Global.asax? Request is not valid there. On a related note, is there...
3
by: rss | last post by:
set rowcount 1 select Idnum1,Idnum3 from mytable order by Idnum1 DESC, idnum3 ASC is equivelent to select first(Idnum1),first(idnum3) from mytable order by first(Idnum1) DESC...
7
by: lathamik | last post by:
Does anyone know of the vb.net equivelent to the c# continue so I can exit the immediate iteration of a for loop and continue with the next one.
23
by: pirata | last post by:
I'm a bit confusing about whether "is not" equivelent to "!=" if a != b: ... if a is not b: ... What's the difference between "is not" and "!=" or they are the same thing?
21
by: Mike N. | last post by:
Can someone tell me if there is a C# equivelent to the VBA 'with' statement that works like this: Set myControl = CommandBars(PopUpToUse).Controls.Add(msoControlButton, before:=5) With...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.