Raised This Month: $ Target: $400
 0% 

how to create isStringFloat?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
grs4
Senior Member
Join Date: Dec 2010
Location: Poland
Old 03-02-2017 , 09:17   how to create isStringFloat?
Reply With Quote #1

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?

Last edited by grs4; 03-02-2017 at 09:18.
grs4 is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 03-02-2017 , 09:20   Re: how to create isStringFloat?
Reply With Quote #2

PHP Code:
if(str_to_float(szText)) 
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
grs4
Senior Member
Join Date: Dec 2010
Location: Poland
Old 03-02-2017 , 09:23   Re: how to create isStringFloat?
Reply With Quote #3

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'
grs4 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 03-02-2017 , 09:24   Re: how to create isStringFloat?
Reply With Quote #4

Quote:
Originally Posted by OciXCrom View Post
PHP Code:
if(str_to_float(szText)) 
But "0.0" is also a floating point value.

There's is_str_float() in String Stocks.
klippy is offline
grs4
Senior Member
Join Date: Dec 2010
Location: Poland
Old 03-02-2017 , 09:25   Re: how to create isStringFloat?
Reply With Quote #5

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
grs4 is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 03-03-2017 , 15:16   Re: how to create isStringFloat?
Reply With Quote #6

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 !!!
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 03-03-2017 at 17:59.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
grs4
Senior Member
Join Date: Dec 2010
Location: Poland
Old 03-03-2017 , 15:26   Re: how to create isStringFloat?
Reply With Quote #7

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
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>

Last edited by grs4; 03-03-2017 at 16:16.
grs4 is offline
Old 03-03-2017, 15:38
Natsheh
This message has been deleted by Natsheh.
Natsheh
Veteran Member
Join Date: Sep 2012
Old 03-03-2017 , 16:20   Re: how to create isStringFloat?
Reply With Quote #8

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
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 03-03-2017 at 16:35.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
grs4
Senior Member
Join Date: Dec 2010
Location: Poland
Old 03-03-2017 , 16:52   Re: how to create isStringFloat?
Reply With Quote #9

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 )

Last edited by grs4; 03-03-2017 at 16:54.
grs4 is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 03-03-2017 , 18:00   Re: how to create isStringFloat?
Reply With Quote #10

Quote:
Originally Posted by grs4 View Post
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
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 03-04-2017 at 09:13.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 07:04.


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