SA-MP Forums Archive
int to float, how can that be done correctly ? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: int to float, how can that be done correctly ? (/showthread.php?tid=91080)



int to float, how can that be done correctly ? - PKRanger - 12.08.2009

Wel lets take an example:

new miliseconds = 5005; // 500.5 seconds
new floateconds = miliseconds / 10; // should return 500,5

But instead of returning 500.5 / 500,5 it returns 0.00000

How can this be done correctly ?

PKR


Re: int to float, how can that be done correctly ? - WrathOfGenesis - 12.08.2009

From what i can see it should work.


Re: int to float, how can that be done correctly ? - paytas - 12.08.2009

First, 5005 milliseconds = 5.005 seconds.

Then, it's

new Floateconds = milliseconds / 1000.0


Re: int to float, how can that be done correctly ? - WrathOfGenesis - 12.08.2009

I think what he put at the top was just an example.


Re: int to float, how can that be done correctly ? - PKRanger - 12.08.2009

new tmp[128];
new float:temp = timechallange / 10;
format(tmp, sizeof tmp, "The timechallange record is %f Seconds, type /time to start a time challange!\n", temp);
print (tmp);
gTextDraw = TextDrawCreate(0.0, 380.0, tmp);
TextDrawUseBox(gTextDraw, 1);

returns:
The timechallange record is 0.000000 Seconds, type /time to start a time challange!

(if i put timechallange in the format instead of temp it just gives 2000.., so the var/int timechallange is reachable..)


Re: int to float, how can that be done correctly ? - paytas - 12.08.2009

I pointed out your mistakes in my post, that you apparently totally ignored.


Re: int to float, how can that be done correctly ? - PKRanger - 12.08.2009

Thnx / 10.0 xD

now, is there any way to remove the extra zero's?

now i have 200.000000. 200.0 should be fine


Re: int to float, how can that be done correctly ? - Nero_3D - 12.08.2009

No you cant remove the extra zero's, floats are always saved with 8 integers behind the comma
But you can denotes how much are shown within the format

%X.Yf

X = denotes how much integer should be before the comma
Y = denotes how much integer should be after the comma

Example

What you want: "%.1f Seconds"

Other Example: "%4.4f Seconds"


Re: int to float, how can that be done correctly ? - PKRanger - 12.08.2009

excellent, thank you :P