Search Unity

[SOLVED] typecast float to int in javascript?

Discussion in 'iOS and tvOS' started by EricJ13, Sep 18, 2009.

  1. EricJ13

    EricJ13

    Joined:
    Feb 28, 2009
    Posts:
    355
    A forum search has lead me to think it's not doable.

    "as" only works on reference types and Unity doesn't dig

    Code (csharp):
    1.     var f1 : float = 3.14;
    2.     var myVar : int = (int)f1;
    or

    Code (csharp):
    1.     var f1 : float = 3.14;
    2.     var myVar : int = int(f1);
    If anyone knows a typecast method that work's I'd appreciate a "how to".
     
    DBarlok likes this.
  2. jmunozar

    jmunozar

    Joined:
    Jun 23, 2008
    Posts:
    1,091
    Hello

    On JS use parseInt(yourFloatHere)

    so it will be something like this:
    Code (csharp):
    1.  
    2. var f1 : float = 3.15;
    3. var i1 : int = parseInt(f1);
    4.  
    if it doesnt work, try with ParseInt() cant remember well if its lower or upper case :p

    hope it helps ;)!!!
     
    DBarlok and ReubenX like this.
  3. EricJ13

    EricJ13

    Joined:
    Feb 28, 2009
    Posts:
    355
    You dah man! Thanx.

    I'm used to parseInt() taking a String argument, not any other data type. So I did try the rather circuitous:

    Code (csharp):
    1.     var f1 : float = 3.14;
    2.     var fs : String = f1.ToString();
    3.     var i1 : int = parseInt(fs);
    Thing is, ToString() doesn't seem to work on floats, trying to access the fs var from the code above throws a NullReferenceException. ToString() does work on the int type. The following works fine:

    Code (csharp):
    1.     var i1 : int = 3;
    2.     var is : String = i1.ToString();
    3.     var i2 : int = parseInt(is);
    Of course that's redundant; why go through the trouble if i1 and i2 are the same. :roll: But it illustrates the point.

    Note that parseInt() truncates a float so in:

    Code (csharp):
    1.     var f1 : float = 3.14;
    2.     var f2 : float = 3.99;
    3.     var i1 : int = parseInt(f1);
    4.     var i2 : int = parseInt(f2);
    i1 and i2 both = 3. In the project I'm working on that doesn't matter, but if rounding is required:

    Code (csharp):
    1.     var f1 : float = 3.14;
    2.     var f2 : float = 3.99;
    3.     var i1 : int = parseInt(Mathf.Round(f1));
    4.     var i2 : int = parseInt(Mathf.Round(f2));
    results in i1 = 3 and i2 = 4.

    Thanx again for the info, you saved the day!
     
    DBarlok likes this.
  4. ckollars

    ckollars

    Joined:
    Aug 9, 2013
    Posts:
    1
    In Javascript all numbers are stored as floats. Internally there's no such thing as an "int" as usually understood. (Also, Javascript is a "loosely typed" language, so it doesn't have a concept of "typecasting" in any case.) In Javascript, what seems to the user to be an integer is really just a floating point number with nothing meaningful after the decimal point.

    The easiest way to produce something that acts pretty much like an int [while avoiding the transition to a string and back again of parseInt()] is to use the "round" function, something like this:
    Depending on what you're doing, Math.floor(...) or Math.ceil(...) may be more appropriate than Math.round(...).
     
    DBarlok likes this.
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Wrong-o. You're on a Unity forum, and Unity doesn't use web Javascript. None of what you wrote is true when it comes to Unity. Namely: int certainly exists, it's not loosely typed, there is typecasting, and there is no Math.round (Mathf.Round, rather). This is why "Javascript" is frequently referred to as Unityscript, since it's a custom language and has many significant differences compared to Javascript. You'd be better off thinking of it as being like ActionScript3, though there are still a number of differences compared to that.

    --Eric
     
    DBarlok likes this.