AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Value float or Integer? (https://forums.alliedmods.net/showthread.php?t=185005)

Larcyn 05-11-2012 17:10

Value float or Integer?
 
Hello,

How can I check if a value is a float or if it's an integer?

I need something similar like the PHP Function: is_float($x) and is_int($x)
http://php.net/manual/en/function.is-float.php
http://php.net/manual/en/function.is-int.php

Thanks in advance

Arkshine 05-11-2012 17:15

Re: Value float or Integer?
 
Give a context/code where you need that.

Exolent[jNr] 05-11-2012 17:25

Re: Value float or Integer?
 
Situation #1:
Code:
someFunction(any:value) {     // Do something with value }
Code:
// Call with an integer someFunction(1337); // Call with a float someFunction(13.37);

In this case, you would need to add another parameter to specify if the provided value is float or integer.

Situation #2:
Trying to determine if a string is a float or integer.

Code:
stock bool:is_str_float(const string[], bool:require_decimal=true) {     new count, pos, ch;     while((ch = string[pos++])) {         if(ch == '.') {             if(++count > 1) {                 return false;             }         }         else if(!('0' <= ch <= '9')) {             return false;         }     }     return (!require_decimal || count == 1); }

Larcyn 05-12-2012 06:19

Re: Value float or Integer?
 
Thanks for your replys, but these can't help me. I should provide more information I guess.

I'm trying to add it into a calculator I made, http://forums.alliedmods.net/showthread.php?t=172415

PHP Code:

format(szArgcharsmax(szArg), "%d"mathEvaluateEquation(szArg)) 

I want to check if the output of mathEvaluateEquation(szArg) is an integer value or if it's a float so I can print out the correct value when a player is dividing something which will contain decimals.

Xalus 05-12-2012 07:27

Re: Value float or Integer?
 
In CS it could be something like:

PHP Code:

new Float:Amount 5.2
if( Amount == float(floatround(Amount)) ) 

floatround will make Amount to 5, float makes it back to 5.0
so: 5.2 != 5.0

Means its a float

claudiuhks 05-12-2012 07:39

Re: Value float or Integer?
 
You forgot an ), Exolent :)

fysiks 05-12-2012 12:59

Re: Value float or Integer?
 
Quote:

Originally Posted by Larcyn (Post 1707282)
Thanks for your replys, but these can't help me. I should provide more information I guess.

I'm trying to add it into a calculator I made, http://forums.alliedmods.net/showthread.php?t=172415

PHP Code:

format(szArgcharsmax(szArg), "%d"mathEvaluateEquation(szArg)) 

I want to check if the output of mathEvaluateEquation(szArg) is an integer value or if it's a float so I can print out the correct value when a player is dividing something which will contain decimals.

A function like that should always return a float and therefore you can always use something like "%.2f". You should post that entire mathEvaluateEquation() function if you want more accurate help.

Larcyn 05-12-2012 16:16

Re: Value float or Integer?
 
Quote:

Originally Posted by fysiks (Post 1707468)
A function like that should always return a float and therefore you can always use something like "%.2f". You should post that entire mathEvaluateEquation() function if you want more accurate help.

I am aware that I can use %.2f in this case, but it would be better if i just had plain numbers instead of decimals when it's an integer value.

fysiks 05-13-2012 02:20

Re: Value float or Integer?
 
Quote:

Originally Posted by Larcyn (Post 1707639)
I am aware that I can use %.2f in this case, but it would be better if i just had plain numbers instead of decimals when it's an integer value.

Did you try Xalus' method?


All times are GMT -4. The time now is 00:24.

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