Home AMX User Forum NetLinx Studio
Options

Converting a Float to an Integer

Greetings,

I need to convert a float variable to an integer. Whether the float value rounds up or down to the integer is of no consequence.

Any ideas? Thanks.

Comments

  • Options
    Joe HebertJoe Hebert Posts: 2,159
    I need to convert a float variable to an integer. Whether the float value rounds up or down to the integer is of no consequence.

    DEFINE_VARIABLE
    FLOAT fFloat
    INTEGER nInteger

    And then just type_cast when you need to like this:

    nInteger = TYPE_CAST(fFloat)
  • Options
    viningvining Posts: 4,368
    If you're doing math with the floats it's recommended (if I recall correctly) to first covert the floats to whole numbers, then do the math and then convert back. In your case you would then type_cast the result.

    So if you wanted 2.4 x 10.8
    Then
    2.4 x 10 = 24
    10.8 x 10 = 108
    Then
    24 x 108 = 2592
    2592 / 10 = 259.2
    Now to convert the float result to an integer type_cast the result.

    This is all fuzzy to me but it has something to do with the way Netlinx handles floating decimal pointers. Apparently it doesn't handle them well.

    If you search for floats or floating you should fine numerous posts about this.
  • Options
    PhreaKPhreaK Posts: 966
    Might be worth doing a sanity check to make sure that your float values are within int ranges before just type casting otherwise things may start to get funky. Have a look at the NCL math.axi for floor, ceiling and rounding functions if you need them.
  • Options
    jweatherjweather Posts: 320
    vining wrote: »
    So if you wanted 2.4 x 10.8
    Then
    2.4 x 10 = 24
    10.8 x 10 = 108
    Then
    24 x 108 = 2592
    2592 / 10 = 259.2

    Except that 2.4 x 10.8 is 25.92... maybe you should let the processor handle the decimal places instead. I'm not aware of any issues with floating point numbers that would be solved by using fixed point math.
  • Options
    viningvining Posts: 4,368
    jweather wrote: »
    Except that 2.4 x 10.8 is 25.92... maybe you should let the processor handle the decimal places instead. I'm not aware of any issues with floating point numbers that would be solved by using fixed point math.
    Yeah I saw the typo this morning but figured most folks would understand. As far as the floating pointer issue goes it was just a re-collection from years ago and the resolution was I beleive as stated. I actually think it may have been a post of yours in response to a problem I was having but I don't recall the circumstances and it's quite possible I didn't "recall correctly" hence the disclaimer and the suggestion to do a search.
  • Options
    a_riot42a_riot42 Posts: 1,624
    Greetings,

    I need to convert a float variable to an integer. Whether the float value rounds up or down to the integer is of no consequence.

    Any ideas? Thanks.

    format is an option as well.

    atoi(format('%3.0f',fFloat)) might do the trick although I haven't used in this way so ymmv.
    Paul
  • Options
    pikopiko Posts: 9
    sorry , but how to do the opposite Integer to float ?
Sign In or Register to comment.