PDA

View Full Version : Stupid JScript "integer" question (All)



cprasley
01-11-2003, 12:58 PM
What is the definitive way to cast a float into an integer in JScript?

adamdelves
01-14-2003, 03:44 PM
Javascript is un-typed. A variable can hold any data type. But you can turn the number into a string and cast it though. Seen as casting the number 12.56 is like cutting off all characters after the decimal place a function that converts the number to a stirng and back to a number again can be used:
<pre>
&lt;script&gt;
Number.prototype.intCast = function () {
var strVal = this.toString();
var strRet = '';

for(i = 0; strVal.charAt(i) != '.' && i &lt; strVal.length; i++)
strRet += strVal.charAt(i);

return new Number(strRet);
}

var num = new Number(1234.4666);

document.write(num.intCast());
&lt;/script&gt;
</pre>

cnm
01-26-2003, 10:25 PM
That will truncate it - is that what you want? Why not just multiply by 100.

adamdelves
01-27-2003, 12:20 AM
Yeah - because that's what happens whan you casat a number to an integer. Look at a VB's fix function it literaly truncates the number. Returnng just the integer portion. What does multiplying by 100 do?

cprasley
04-18-2003, 02:40 PM
I am trying to convert latitude/longitude floating point values into their degree-minute-seconds variant. With VB I would do:
Q=sgn(X)
X=abs(X)
D=int(X); X=(X-D)*60.0
M=int(X); X=(X-M)*60.0
S=int(X+0.5)
D=D*Q

Since JScript doesn't have an int() function I was looking for a simple substitute. I thill find it hard to believe that JScript is so broken....

Mosaic1
04-18-2003, 04:38 PM
Which version are you using? The JS parseInt function will do the same as VB. Which is round down aka cut off. 1.7 = 1
1.2 =1

var WSHShell = WScript.CreateObject("WScript.Shell");
var Mynumber = parseInt(1.78);
WSHShell.Popup(Mynumber);


Not good. You might consider an alternative. A conditional statement to evaluate the decimal. And then change the value accordingly.

Something like this pseudo code:(jscript is not my strong suit. This looks more like VB. Because it is neither ! LOL)


var y = .49
var x = 1.23
If ParseInt(x + y) is greater than ParseInt(x)
x = ParseInt(x + y)
else x = ParseInt(x)

Anything .51 or greater will be rounded up. But if you do not want that, use parseInt

I use Windows Script 5.6

Mosaic1
04-18-2003, 05:17 PM
Or this string manipulation which again doesn't evaluate the number. It just cuts it off at the decimal point.

var WSHShell = WScript.CreateObject("WScript.Shell");
var X = new String(96.2345);
var Q = X.indexOf(".");
var Z = X.substring(0,Q);
//Convert the string back to a number
var Final = new Number(Z);
WSHShell.Popup(Final);

Mosaic1
04-19-2003, 11:16 AM
I wrote you a function to do what you want with any number. It will round up or down depending on the value of the decimal. I included usage and examples of rounding up and down.

// javascript to round a float up or down
var WSHShell = WScript.CreateObject("WScript.Shell");

function My(x){

var y = .49
if (parseInt(x + y) &gt; parseInt(x)){
return x = parseInt(x +y)
}
else return x = parseInt(x);
}

//example of use My(any number)
WSHShell.Popup(My(6.321));
var P = My(3.567) + 10;
WSHShell.Popup(P);



Copy and paste the bold to Notepad. Save as round.js

EDIT: DUH or just use this like you did in your VB !

parseInt(x + .5)



<P ID="edit"><FONT class="small">Edited by Mosaic1 on 04/19/03 10:53.</FONT></P>