Sign in to follow this  
Naskot

How round a decimal in a script?

Recommended Posts

Hi, I'm looking to round a value in a script

Example 1.32758 to 1.33.

I know only <Int>,

ratio = int (ratio);

but this latter rounded only at 1
I can not find a value, or how to formulate it in a script.


Any ideas ?

Edited by Naskot

Share this post


Link to post
Share on other sites
11 minutes ago, dpj said:

int(val * 100 + 0.5) / 100

Thank you for your answer, but I already tried it, it does not work, I get an error

******* script runtime error *******
pair 'Your Ratio K/D: ' and 'undefined' has unmatching types 'string' and 'undefined': (file 'plugins/customcommands.gsc', line 149)
   self iprintlnbold( "Your Ratio K/D: "+ ratio );
                                            *
called from:
(file 'maps/mp/gametypes/_globallogic.gsc', line 697)
 self thread plugins\customcommands::DBLookup();
             *
started from:
(file 'maps/mp/gametypes/_globallogic.gsc', line 673)
 waittillframeend;
 *
************************************    

The script:

            ratio = 0;
            data = getdata( "ratio", self getGuid() );    
            ratio = data[ "ratio" ];  
            ratio = int(ratio * 100 + 0.5) / 100;
            self iprintlnbold( "^1Your Ratio K/D: ^7"+ ratio );    

Edited by Naskot

Share this post


Link to post
Share on other sites
18 minutes ago, dpj said:

then, step by step, figure out what the issue is^^

I admit that I do not understand me either ^^,

but here is what I understand in the error, it would be that the variable <ratio> would not be declared yet,but the latter is declared, just above, but declared all the same, I do not understand this message:

pair 'undefined' and '100' has unmatching types 'undefined' and 'int':

but if I concise the script in this way, everything works, but I only get <1> as a result, not easy to make a ratio with only 1 ^^

            ratio = 0;
            data = getdata( "ratio", self getGuid() );    
            ratio = data[ "ratio" ];  
            ratio = int(ratio);
            self iprintlnbold( "^1Your Ratio K/D: ^7"+ ratio );    

seems that int () does not accept anything other than a variable

 

Cordially

 

Edited by Naskot

Share this post


Link to post
Share on other sites
2 minutes ago, Naskot said:

pair 'undefined' and '100' has unmatching types 'undefined' and 'int':

ratio is multiplied by 100. apparently ratio is undefined.

Share this post


Link to post
Share on other sites
20 minutes ago, dpj said:

ratio is multiplied by 100. apparently ratio is undefined.

I tried it

            ratio = 1.24621;
            //data = getdata( "ratio", self getGuid() );    
            //ratio = data[ "ratio" ];  
            ratioint = int(ratio * 100 + 0.5) / 100;
            self iprintlnbold( "^1Your Ratio K/D: ^7"+ ratioint );    

i get <Your Ratio K/D:1.25> and no error but if i write it :

            ratio = 1.24621;
            data = getdata( "ratio", self getGuid() );    
            ratio = data[ "ratio" ];  
            ratioint = int(ratio * 100 + 0.5) / 100;
            self iprintlnbold( "^1Your Ratio K/D: ^7"+ ratioint );    

i get this error: pair 'undefined' and '100' has unmatching types 'undefined' and 'int':

there seems to be something missing in the variables of my database yet the ratio used ratio = 1.24621; is the same as my mysql database,  

I give more detail of my script, I don't understand the error

 

getData( data, id )
{
    query = "SELECT S." + data+ " FROM clients AS C,xlr_playerstats AS S WHERE C.id = S.client_id  AND C.guid = " + id + " ";
    
    mysql_query( game[ "mysql" ], query );
    
    if( mysql_num_rows( game[ "mysql" ] ) != 1 )
        return undefined;
    
    return mysql_fetch_row( game[ "mysql" ] );
}

MysqlRatio()
{
            ratio = 1.24621;
            data = getdata( "ratio", self getGuid() );    
            ratio = data[ "ratio" ];  
            ratioint = int(ratio * 100 + 0.5) / 100;
            self iprintlnbold( "^1Your Ratio K/D: ^7"+ ratioint );    
}

 

Edited by Naskot

Share this post


Link to post
Share on other sites
7 minutes ago, leiizko said:

Your query is returning nothing / is throwing error. Test the queries beforehand.

I tested the query mysql send me a data, i have the problem of the moment or I try to add or multiply int ()

 

If i write only it, i get Your Ratio K/D: 1.24621

 MysqlRatio()
{

           data = getdata( "ratio", self getGuid() );    
            ratio = data[ "ratio" ];  
            self iprintlnbold( "^1Your Ratio K/D: ^7"+ ratio );    

}

Edited by Naskot

Share this post


Link to post
Share on other sites

sigh... 

18 minutes ago, Naskot said:

getData( data, id )
{
    query = "SELECT S." + data+ " FROM clients AS C,xlr_playerstats AS S WHERE C.id = S.client_id  AND C.guid = " + id + " ";
    
    mysql_query( game[ "mysql" ], query );
    
    if( mysql_num_rows( game[ "mysql" ] ) != 1 )
        return undefined;           <---------------------------------------------------- HERE
    
    return mysql_fetch_row( game[ "mysql" ] ); 
}

 

19 minutes ago, Naskot said:

data = getdata( "ratio", self getGuid() );    <--------------- RETURN UNDEFINED!
 ratio = data[ "ratio" ];  <---- THUS UNDEFINED

Also if column has a value of NULL the value will obviously be again undefined. So AGAIN - check the query.

Share this post


Link to post
Share on other sites
3 minutes ago, leiizko said:

sigh... 

 

Also if column has a value of NULL the value will obviously be again undefined. So AGAIN - check the query.

If i write only it, i get Your Ratio K/D: 1.24621 without error

 MysqlRatio()
{

           data = getdata( "ratio", self getGuid() );    
            ratio = data[ "ratio" ];  
            self iprintlnbold( "^1Your Ratio K/D: ^7"+ ratio );    

}

so he can not to be UNDEFINED

Edited by Naskot

Share this post


Link to post
Share on other sites

then test the rounding part step by step. how hard can it be xD 

Share this post


Link to post
Share on other sites

The mysql database send me the good data, i get 1.24621  on the variable ratio, but i want change it to 1.25, so i want used int(), i get an error.

 

i tried it also:

 

getData( data, id )
{
    query = "SELECT S." + data+ " FROM clients AS C,xlr_playerstats AS S WHERE C.id = S.client_id  AND C.guid = " + id + " ";
    
    mysql_query( game[ "mysql" ], query );
   
    return mysql_fetch_row( game[ "mysql" ] ); 
}

 MysqlRatio()
{

           data = getdata( "ratio", self getGuid() );    
            ratio = data[ "ratio" ];  

             self iprintlnbold( "^1I verify than DB send me data, so Ok its work: ^7"+ ratio );    

           ratio = int(ratio * 100 + 0.5) / 100;
            self iprintlnbold( "^1But here i want not, and write an error: ^7"+ ratio );    

}

 

it makes me crazy this story ^^

Edited by Naskot

Share this post


Link to post
Share on other sites
48 minutes ago, dpj said:

then test the rounding part step by step. how hard can it be xD 

Thanks for the help, even if it's a total failure this integration of a ratio from xlrstats,

unless you know how to change a text variable to a numeric variable.

Cordially

54 minutes ago, leiizko said:

sigh... 

 

Also if column has a value of NULL the value will obviously be again undefined. So AGAIN - check the query.

I found the reason, I do not know how to solve this problem.

The cause is that the <ratio> variable returns a non-numeric value, therefore it is not possible to process it via an addition or multiplication.

I also checked without treatment by mysql the following syntax:

ratio = 1.12345;
ratio = int (ratio * 100 + 0.5) / 100;
self iprintlnbold (ratio); // return 1.12

but I use the following syntax, it returns the error <pair 'undefined' and '100' has unmatching types 'undefined' and 'int':>

ratio = "1.12345";
ratio = int (ratio * 100 + 0.5) / 100; // return an error
self iprintlnbold (ratio); // show nothing

 

try it you will see that it will not work, thanks anyway

 

Edited by Naskot

Share this post


Link to post
Share on other sites

have you tried making it into a number then? with float() or int() ?

... or just return a formatted string with sql. 

Share this post


Link to post
Share on other sites
12 minutes ago, dpj said:

have you tried making it into a number then? with float() or int() ?

... or just return a formatted string with sql. 

float () returns to me unknown function in scripts, Round () is not available on GSC, but available only on C ++ or C language.

For the mysql xlr_playerstats table, the ratio column structure is float, if I change this or I change the size, I may encounter problems with Bigbrotherbot, or xlrstats website.

the only way would be that in scripts I load a data into a numeric variable as being, but I do not know how to do

 

Share this post


Link to post
Share on other sites
2 minutes ago, Naskot said:

Round () is not available on GSC, but available only on C ++ or C language.

rounding is not your issue. your issue is that the returned number is interpreted as a string instead of a number. 

3 minutes ago, Naskot said:

float () returns to me unknown function in scripts

its there https://github.com/callofduty4x/CoD4x_Server/blob/bd70805742f510ac76b1d979bc4f66716ffb4da1/src/scr_vm_functions.c#L3336
maybe not in the server version you are using

4 minutes ago, Naskot said:

For the mysql xlr_playerstats table, the ratio column structure is float, if I change this or I change the size, I may encounter problems with Bigbrotherbot, or xlrstats website.

thats a bs idea indeed. what i suggested is changing the select statement. 

 

anyways, casting the value with int() (if float() isn't available for you) would be the easiest way. 

Share this post


Link to post
Share on other sites

If the return is string then you're good to go lol. Use getSubStr() function.

 

And just a side note: It would be a lot easier and faster for us if you just said at the begining the error was something in lines of "unmatching types 'string' and 'int':"

Edited by leiizko

Share this post


Link to post
Share on other sites

I guess he's laughing.

You returning undefined from a function and not get expect of it. Is it ok for you?

If it's not  then make use of `isDefined(...)` function.

 

Share this post


Link to post
Share on other sites
On 18/8/2018 at 9:38 PM, leiizko said:

If the return is string then you're good to go lol. Use getSubStr() function.

 

And just a side note: It would be a lot easier and faster for us if you just said at the begining the error was something in lines of "unmatching types 'string' and 'int':"

Hi,

your solution is not pretty, I solved part of the problem with the function round () mysql, function that did not work in the first script but with the other script is functional, it does not work definitely not with int ().

depressing, now I realize that the plugin mysql and mod lepko gungame 1.2 triggers me a:

This program has been crashed with signal: Segmentation fault.

the server is running but crashing too often.

sqlquery = "SELECT C.id,round(S.ratio, 2),round(S.skill) FROM " + level.dvar[ "db_clients" ] + " AS C," + level.dvar[ "db_xlrplayerstats" ] + " AS S WHERE C.id = S.client_id  AND C.guid = " + self getGuid() + " ";
 

script on pastebin, because this website crash when i insert source code

https://pastebin.com/vuASzrWs

On 19/8/2018 at 9:27 PM, volkv said:

ratio = int( self.pers[ "kills" ] / self.pers[ "deaths" ] * 100 ) / 100;

 

 

Hi

the project is to recover the ratio / skill of xlrstats on Bigbrotherbot, this type of script is only for the game in progress,

Nb: did you read the message that I sent you in private?, Volkv

Share this post


Link to post
Share on other sites
On 18.08.2018 at 7:22 PM, Naskot said:

The script:

            ratio = 0;
            data = getdata( "ratio", self getGuid() );    
            ratio = data[ "ratio" ];  
            ratio = int(ratio * 100 + 0.5) / 100;
            self iprintlnbold( "^1Your Ratio K/D: ^7"+ ratio );  

   Hello! Try using this option in the script:

 data = getdata( "deaths", self getGuid() );
    self.pers["deaths"] = data[ "deaths" ];
    data = getdata( "kills", self getGuid() );
    self.pers["kills"] = data[ "kills" ];
    
    self.pers[ "ratio" ] = int( int( self.pers[ "kills" ] ) / int( self.pers[ "deaths" ] ) * 100 ) / 100;

Share this post


Link to post
Share on other sites
46 minutes ago, a6pakaTa6pa said:

   Hello! Try using this option in the script:

 data = getdata( "deaths", self getGuid() );
    self.pers["deaths"] = data[ "deaths" ];
    data = getdata( "kills", self getGuid() );
    self.pers["kills"] = data[ "kills" ];
    
    self.pers[ "ratio" ] = int( int( self.pers[ "kills" ] ) / int( self.pers[ "deaths" ] ) * 100 ) / 100;

Hello,
this request allows you to introduce, if I understand correctly, the tables of mysql data of xlrstats in the script that Volkv proposed to me, then in this last one to calculate the ratio.

To get the ratio, it is already in the ratio column of xlrstats, already calculated just enough to reduce to 2 decimal, then rounds up, with the function round ()

sqlquery = "SELECT C.id, round (S.ratio, 2), round (S.skill) FROM" + level.dvar ["db_clients"] + "AS C," + level.dvar ["db_xlrplayerstats"] + "AS S WHERE C.id = S.client_id AND C.guid =" + self getGuid () + "";

Thanks anyway

Share this post


Link to post
Share on other sites
3 hours ago, dpj said:

fyi that's truncation, not rounding.

hi, no its not a truncation but a rounding,

this is my ratio and skill

my ratio in the database is 1.25968 / in game its rounding at 1.26 so a rounding

and the skill 1509.7    in the database is well rounding at 1510 so again a rounding

Mysql's Round function works in the same way that is used in all applications, including php web sites, for example.

A mysql round function that is used in more than 60% of the planet's websites, including the CMS that is used here, <Invision>.

in any case thanks for the help, the problem is solved.

cordially

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this