[Include] formatex - Improved "format" function!
#19

Here's a neat way to always have nicely formatted money values!

Example:
pawn Код:
printf("You just earned %m. You now have a total of %m in your bank.", 12000, 554279811);
printf("%m", 1000000000);
printf("%m", 100000000);
printf("%m", 10000000);
printf("%m", 1000000);
printf("%m", 100000);
printf("%m", 10000);
printf("%m", 1000);
printf("%m", 100);
printf("%m", 10);
printf("%m", 1);
printf("%m", 0);
Output:
Код:
You just earned $12,000. You now have a total of $554,279,811 in your bank.
$1,000,000,000
$100,000,000
$10,000,000
$1,000,000
$100,000
$10,000
$1,000
$100
$10
$1
$0
Specifier code:
pawn Код:
// Money
FormatSpecifier<'m'>(output[], amount) {
    if (amount) {
        new
            i = 18,
            neg = amount < 0
        ;
       
        // The spaces must be there, otherwise strdel below won't work
        // on lower numbers.
        output = "$                ";
       
        // Null-out the end of it
        output[i] = 0;
       
        if (neg)
            amount = -amount;
       
        // Going right-left, add one number each time
        while (amount) {
            // Add a thousand separator every 3rd time
            if (!((i + 1) % 4))
                output[--i] = ',';
           
            // Now add the last digit of the number
            output[--i] = '0' + (amount % 10);
           
            // Then divide the number by 10, so digit in the end will be gone
            amount /= 10;
        }
       
        // Delete the spaces between the $-sign and the first (last) number
        strdel(output, 1, i);
       
        // Add a minus sign if needed
        if (neg)
            strins(output, "-", 1);
    } else {
        output = "$0";
    }
}
Reply


Messages In This Thread
formatex - Improved "format" function! - by Slice - 26.01.2012, 08:00
Re: formatex - Improved "format" function! - by KingHual - 26.01.2012, 08:02
Re: formatex - Improved "format" function! - by kizla - 26.01.2012, 08:03
Re: formatex - Improved "format" function! - by [HK]Ryder[AN] - 26.01.2012, 08:05
Re: formatex - Improved "format" function! - by Slice - 26.01.2012, 08:10
Re: formatex - Improved "format" function! - by iPLEOMAX - 26.01.2012, 08:19
Re: formatex - Improved "format" function! - by fiki574 - 26.01.2012, 08:30
Re: formatex - Improved "format" function! - by T0pAz - 26.01.2012, 08:37
Re: formatex - Improved "format" function! - by Kaperstone - 26.01.2012, 08:48
Re: formatex - Improved "format" function! - by System64 - 26.01.2012, 09:02
Re: formatex - Improved "format" function! - by Lorenc_ - 26.01.2012, 09:07
Re: formatex - Improved "format" function! - by [Diablo] - 26.01.2012, 10:56
Re: formatex - Improved "format" function! - by [XST]O_x - 26.01.2012, 13:46
Re: formatex - Improved "format" function! - by hossa - 26.01.2012, 14:13
Re: formatex - Improved "format" function! - by iZN - 26.01.2012, 14:24
Re: formatex - Improved "format" function! - by TheArcher - 26.01.2012, 16:29
Re: formatex - Improved "format" function! - by TheBetaFox - 26.01.2012, 16:39
Respuesta: formatex - Improved "format" function! - by [Nikk] - 26.01.2012, 16:49
Re: formatex - Improved "format" function! - by Slice - 29.01.2012, 13:32
Re: formatex - Improved "format" function! - by Niko_boy - 29.01.2012, 14:26
Re: formatex - Improved "format" function! - by DeathTone - 19.02.2012, 02:00
Re: formatex - Improved "format" function! - by Slice - 19.02.2012, 10:33
Re: formatex - Improved "format" function! - by Shelby - 19.02.2012, 23:05
Re: formatex - Improved "format" function! - by shitbird - 21.02.2012, 15:03
Re: formatex - Improved "format" function! - by Marricio - 21.02.2012, 19:54
Re: formatex - Improved "format" function! - by Joey^ - 03.03.2012, 16:49
Re: formatex - Improved "format" function! - by Niko_boy - 04.06.2012, 14:28
Re: formatex - Improved "format" function! - by Slice - 04.06.2012, 14:40
Re: formatex - Improved "format" function! - by Slice - 04.06.2012, 16:35
Re: formatex - Improved "format" function! - by QuaTTrO - 22.07.2013, 07:33
Re: formatex - Improved "format" function! - by Omirrow - 23.07.2013, 19:38
Re: formatex - Improved "format" function! - by Darnell - 24.07.2013, 13:07
Re: formatex - Improved "format" function! - by Ryan_Bowe - 14.03.2015, 20:55
Re: formatex - Improved "format" function! - by Dayvison_ - 01.04.2016, 19:17
Re: formatex - Improved "format" function! - by bsHoX - 03.04.2016, 03:18
Re: formatex - Improved "format" function! - by AndySedeyn - 02.10.2016, 10:02
Re: formatex - Improved "format" function! - by Adiiti - 20.08.2017, 14:55

Forum Jump:


Users browsing this thread: 1 Guest(s)