SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 20/08/2019, 09:09 PM   #1
Oficer
Big Clucker
 
Join Date: Sep 2015
Posts: 91
Reputation: 0
Default Scanf - formating numbers

Hi, how I can formating numbers with "." ?
For Example: 20000 to "20.000"
Oficer is offline   Reply With Quote
Old 20/08/2019, 09:22 PM   #2
xRadical3
Huge Clucker
 
xRadical3's Avatar
 
Join Date: May 2017
Location: Iran
Posts: 459
Reputation: 17
Default Re: Scanf - formating numbers

pawn Code:
stock f(numbers)
{
    new temp[24],counter = -1;
    valstr(temp,numbers);
    for(new i = strlen(temp);i > 0; i--)
    {
        counter++;
        if(counter == 3)
        {
            strins(temp,".",i);
            counter = 0;
        }
    }
    return temp;
}
//printf("%s", f(100000));
//Print: 100.000
xRadical3 is online now   Reply With Quote
Old 20/08/2019, 11:41 PM   #3
CheezIt
Big Clucker
 
CheezIt's Avatar
 
Join Date: Apr 2017
Posts: 178
Reputation: 17
Default Re: Scanf - formating numbers

pawn Code:
FormatNumber(number, const separator[] = ".")
{
    new string[16];
    valstr(string, number);

    for(new i = strlen(string) - 3; i > 0; i -= 3)
    {
        strins(string, separator, i);
    }
    return string;
}
__________________
I'd like to bring back my old CNR server that was created from scratch & hosted years ago. I'll host & update everything (including the website & forum). You would pay for hosted tab and help manage the community. PM me if you're interested!

Last edited by CheezIt; 21/08/2019 at 04:50 PM.
CheezIt is offline   Reply With Quote
Old 21/08/2019, 08:23 AM   #4
Calisthenics
High-roller
 
Join Date: May 2018
Posts: 1,229
Reputation: 205
Default Re: Scanf - formating numbers

Both functions above fail on negative numbers such -100000

pawn Code:
FormatNumber(number, const separator[] = ".")
{
    new string[16];

    format(string, sizeof (string), "%d", number); // valstr can crash the server with high values

    for (new i = strlen(string) - 3, j = number < 0 ? 1 : 0; i > j; i -= 3)
    {
        strins(string, separator, i, sizeof (string));
    }
    return string;
}
Or https://forum.sa-mp.com/showthread.php?t=184328
Calisthenics is offline   Reply With Quote
Reply

Thread Tools
Display Modes

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
[REP++] Formating long numbers AdmBot Scripting Help 8 12/03/2016 05:45 PM
A little help with scanf and delimiters djshadowxm81 Scripting Help 5 26/01/2014 08:33 PM
scanf + mysql r34 как? vitalik6243 Русский/Russian 5 25/01/2014 08:56 AM
[Ajuda] scanf e strcmp[duvida] unamed Portuguкs/Portuguese 4 21/12/2013 09:13 PM
[+rep] Scanf sanplayer Scripting Help 4 27/02/2012 07:31 PM


All times are GMT. The time now is 09:50 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.