AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   how to create isStringFloat? (https://forums.alliedmods.net/showthread.php?t=294573)

grs4 03-02-2017 09:17

how to create isStringFloat?
 
how to create a function that checks if string is exactly a float?

PHP Code:

stock bool:isStringFloat(szText[], len)
{
    
trim(szText), numline;

    if(
containi(szText".") == -1)
        return 
false;
    
    for(new 
0leni++)
    {
        
line szText[i];

        if(
isdigit(line))
            continue;
        else if(
line == '.')
        {
            if(
num == 1)
                return 
false;
            
num++;
            continue;
        }
        else
            return 
false;
    }
    return 
true;


Is code above a good code?

OciXCrom 03-02-2017 09:20

Re: how to create isStringFloat?
 
PHP Code:

if(str_to_float(szText)) 

:bee:

grs4 03-02-2017 09:23

Re: how to create isStringFloat?
 
or floatstr, yeah, i know, but i remember when i did like this, if i set a string with 'a' a float would be returned as '4124142.42312' or something like that. not '0'

klippy 03-02-2017 09:24

Re: how to create isStringFloat?
 
Quote:

Originally Posted by OciXCrom (Post 2500050)
PHP Code:

if(str_to_float(szText)) 

:bee:

But "0.0" is also a floating point value.

There's is_str_float() in String Stocks.

grs4 03-02-2017 09:25

Re: how to create isStringFloat?
 
PHP Code:

stock bool:is_str_float(const string[])
{
    new 
cip;
    while(
is_char(string[i++]))
    {
        if(!
isdigit(c))
        {
            if(
!= '.' || p)
            {
                return 
false;
            }
            
            
1;
        }
    }
    
    return (
1);



Oh, so just like my code. thanks :)

Natsheh 03-03-2017 15:16

Re: how to create isStringFloat?
 
PHP Code:

stock bool:is_str_float(const sString[])
{
    new 
strlen(sString);
    if(
3) return false;
    
    for(new 
0pi++)
    {
        if(!
isdigit(sString[i]) && sString[i] != '.')
            return 
false;
    }
    
    new 
contain(sString".");
    if(
<= || == (p-1) || contain(sString[i+1],".") > -1) return false;
    
    return 
true;



TESTED AND WORKS CHARMING !!!

grs4 03-03-2017 15:26

Re: how to create isStringFloat?
 
on my eye

For your example
Code:

a5.5a
will return true

and also

Code:

.5
will throw an error. Index out of bounds.


i see you edited code.
Code:

5.5.
will not work :P
why? because "for" loop will pass by, but
PHP Code:

new Contain(sString"."// will return 0
If(<= /*false*/|| == (p-1/*false*/|| contain(sString[i+1],".") > -/*false, because i+1 = 5 */) return false

</span></span>

Natsheh 03-03-2017 16:20

Re: how to create isStringFloat?
 
Have you tested it?

Btw ik i+1 is equal to 5 but the contain native will ignore the previous '.' and start searching for a new one starting from that position which is (i+1 // 5), if it found an extra dot will return false remember contain native handles with a strings not a character

new i = Contain(sString, ".") // will return 0
And why this will return 0 if it based on this 5.5. The correct result will be 1

grs4 03-03-2017 16:52

Re: how to create isStringFloat?
 
I dont test it, because i'm not on pc with amxx

Code:

Btw ik i+1 is equal to 5 but the contain native will ignore the previous  '.' and start searching for a new one starting from that position which  is (i+1 // 5)
Quote:

return: -1 if failure. Any other value indicates a position in the string where the match starts.
If i understand you well, we can't set contain/i start postion for looking match text in PAWN.
Code:

contain(sString[i+1],".")

Will check if "5" contains "." which returns -1 ( false )

Natsheh 03-03-2017 18:00

Re: how to create isStringFloat?
 
Quote:

Originally Posted by grs4 (Post 2500523)
I dont test it, because i'm not on pc with amxx

Code:

Btw ik i+1 is equal to 5 but the contain native will ignore the previous  '.' and start searching for a new one starting from that position which  is (i+1 // 5)
If i understand you well, we can't set contain/i start postion for looking match text in PAWN.
Code:

contain(sString[i+1],".")

Will check if "5" contains "." which returns -1 ( false )

i just tested it and it works very good !



Will check if the string "5." contains "." which will returns 1


All times are GMT -4. The time now is 16:12.

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