AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   truncating float to an int (https://forums.alliedmods.net/showthread.php?t=76150)

MALON 08-19-2008 09:26

truncating float to an int
 
How do I take a float, such as 125.9925 and make it 125 when the trailing decimal size will be unknown?

When I can't typecast from float to an int like in Java or C++, I normally take 125 - (125.9925%1) but I can't do that in pawn because I can't use the modulo operator on floats.

Thanks!

Brad 08-19-2008 09:31

Re: truncating float to an int
 
http://www.amxmodx.org/funcwiki.php?...loat&go=search

That gives you all the functions with "float" in their name.

The specific one you want is: http://www.amxmodx.org/funcwiki.php?go=func&id=73

MALON 08-19-2008 09:53

Re: truncating float to an int
 
Thanks Brad, I looked there a thousand times, but it never occurred to me that floatround_down is the same as truncating.

Last Question:

How can I retrieve just the trailing digits? i.e. 125.9925 and get .9925 (or 9925 with no decimal) Sorry, I'm used to the modulo operator and I feel naked without it now.

danielkza 08-19-2008 12:43

Re: truncating float to an int
 
Quote:

Originally Posted by MALON (Post 672034)
Thanks Brad, I looked there a thousand times, but it never occurred to me that floatround_down is the same as truncating.

Last Question:

How can I retrieve just the trailing digits? i.e. 125.9925 and get .9925 (or 9925 with no decimal) Sorry, I'm used to the modulo operator and I feel naked without it now.

floatfract()

danielkza 08-19-2008 12:51

Re: truncating float to an int
 
Quote:

Originally Posted by XxAvalanchexX (Post 672142)
Pawn has the modulus operator: %

I don't think % works on floats. There's no rest if any of the operands is a float.

Exolent[jNr] 08-19-2008 15:16

Re: truncating float to an int
 
This is a pretty dirty way to do it, but here is my idea:

Code:
new Float:rational; // set rational to your number new integer = floatround(rational, floatround_floor); new Float:decimal = rational - float(integer); /* Example: rational = 32.5 integer = floatround(32.5, floatround_floor) integer = 32 decimal = 32.5 - float(32) decimal = 32.5 - 32.0 decimal = 0.5 */

Arkshine 08-19-2008 15:22

Re: truncating float to an int
 
Like danielkza said, floatfract() exists for that.


All times are GMT -4. The time now is 03:13.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.