Home AMX User Forum AMX General Discussion
Options

Rounding positive numbers?

Hi everyone
I just checked out the AMX tech note about using 'type_cast' for getting a + number rounded to the nearest whole number. Does anyone have a better example of how to do this? Where to put the code in the program, varibles etc. I want to round the run time from a projector to a whole number.

Comments

  • Options
    yuriyuri Posts: 861
    i dont think the technote can be anymore clearer and says it all.
    what happens when you typecast a float with value 2.2 to an integer is that the integer cant contain the decimal part, so its left off.
    To make sure the values in between the x.0 and x.4 regions are being rounded down and the values in between the x.5 and x.9 regions are rounded up you just add 0.5

    example:
    you have your projector variable. I take from your earlier saying that your variable is a float? So let's say 19,2 hours.

    FLOAT fProjectorHours = 19,2
    INTEGER nProjectorHours = TYPE_CAST(fProjectorHours + 0.5)

    now your integer nProjectorHours contains the value 19

    i would reckon only using this 'conversion' for displaying the value, for example on a touchpanel, and keeping the 'original' float value intact...

    hope this helps abit, im not really sure if i explained it in a nice way :p
Sign In or Register to comment.