Raised This Month: $51 Target: $400
 12% 

Read minus and zero number


Post New Thread Reply   
 
Thread Tools Display Modes
Natsheh
Veteran Member
Join Date: Sep 2012
Old 01-14-2020 , 14:26   Re: Read minus and zero number
Reply With Quote #11

PHP Code:
stock is_str_number(const str[])
{
    new 
szValue[20];
    
copy(szValuecharsmax(szValue), str);
    
trim(szValue);
    
    if(
szValue] == '-'szValue[0] = '0';
    return 
is_str_num(szValue);

Use this stock instead.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 01-15-2020 at 15:03.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
georgik57
Veteran Member
Join Date: Oct 2008
Location: 🎧Music World
Old 01-14-2020 , 16:43   Re: Read minus and zero number
Reply With Quote #12

Quote:
Originally Posted by Napoleon_be View Post
why does everyone still hardcore their arrays when charsmax' there to make it easy for you?
Mine is old code. Doesn't really matter anyway pretty much.
__________________
georgik57 is offline
Send a message via MSN to georgik57 Send a message via Yahoo to georgik57 Send a message via Skype™ to georgik57
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-14-2020 , 18:08   Re: Read minus and zero number
Reply With Quote #13

Quote:
Originally Posted by Natsheh View Post
PHP Code:
stock is_str_number(const str[])
{
    new 
szValue[20];
    
copy(szValuecharsmax(szValue), str);
    
trim(szValue);
    new 
contain(str"-");

    if(
> -1szValue[X] = 0;
    return 
is_str_num(szValue);

Use this stock instead.
This is null-terminating the string on the first character if it's a negative number (0 == EOS).
PHP Code:
if(> -1szValue[X] = 0
Change this line to:
PHP Code:
if(> -1szValue[X] = '0'
Not even sure if contain() is needed, why not just do this? The expectation is someone is passing a string holding a number. A trim() is being executed so the minus sign would only be at the left-most position.
PHP Code:
if ( szValue] == '-' 
    
szValue] = '0'
__________________

Last edited by Bugsy; 01-14-2020 at 18:31.
Bugsy is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 01-15-2020 , 15:04   Re: Read minus and zero number
Reply With Quote #14

Quote:
Originally Posted by Bugsy View Post
This is null-terminating the string on the first character if it's a negative number (0 == EOS).
PHP Code:
if(> -1szValue[X] = 0
Change this line to:
PHP Code:
if(> -1szValue[X] = '0'
Not even sure if contain() is needed, why not just do this? The expectation is someone is passing a string holding a number. A trim() is being executed so the minus sign would only be at the left-most position.
PHP Code:
if ( szValue] == '-' 
    
szValue] = '0'
Thanks bugs for some reason I had more possibilities in mind, code optimized.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 01-15-2020 , 21:27   Re: Read minus and zero number
Reply With Quote #15

Quote:
Originally Posted by OnePL View Post
eg. -1, -2, -545335 or 0
is_str_num("0") returns true properly. For negative numbers, it looks like this is a bug that should be reported here.
__________________
fysiks is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-15-2020 , 22:26   Re: Read minus and zero number
Reply With Quote #16

Quote:
Originally Posted by fysiks View Post
is_str_num("0") returns true properly. For negative numbers, it looks like this is a bug that should be reported here.
This is a stock in string_stocks and the description states it returns true if the string contains only digits, so it's 'technically' working as described. An entirely new stock would be needed to maintain reverse compatibility, or an optional 'NegativeSupport' boolean defaulted to false should be added. This should have been written to consider negative numbers.
PHP Code:
/**
 * Returns whether a given string contains only digits.
 * This returns false for zero-length strings.
 *
 * @param sString       Character to test.
 * @return              True if string contains only digit, otherwise false.
 */
stock bool:is_str_num(const sString[])
{
    new 
0;

    while (
sString[i] && isdigit(sString[i]))
    {
        ++
i;
    }

    return 
sString[i] == && != 0;

PHP Code:
stock bool:is_str_num(const sString[] , bool:bNegativeSupport=false)
{
    new 
szTempStr13 ];
    
    
copyszTempStr charsmaxszTempStr ) , sString );
    
    if ( 
bNegativeSupport && ( szTempStr] == '-' ) )
    {
        
szTempStri++ ] = '0';
    }
    
    while ( 
szTempStr[i] && isdigit(szTempStr[i] ) ) 
    {
        ++
i;
    }

    return 
szTempStr[i] == && != 0;

__________________

Last edited by Bugsy; 01-15-2020 at 22:27.
Bugsy is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 01-16-2020 , 20:15   Re: Read minus and zero number
Reply With Quote #17

Ah, I see. I guess thinking that the function names are logical doesn't always work .
__________________
fysiks is offline
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 12:24.


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