How to put punctuation in a sql row

1

Well I'm looking for a way to put punctuation on a rows count that is made from my mysql.

If the returned value is above 1000 it inserts scores, for example:

  • 1000 = 1000
  • 1000000 = 1,000,000

The code is as follows:

void sql_rows(MYSQL_ROW sql) {
   printf("a quantidade de rows é '%s'\n", sql->row[0]);
}

I always get more values without punctuation Is it possible to add a score on the value returned from rows?

    
asked by anonymous 01.04.2017 / 22:50

1 answer

0

You can create a function that writes the number to pieces, and insert the points between them:

void
escrever_numero(unsigned int n, FILE * stream) {
    char buf[10];
    char * ptr = buf;
    size_t nchar, i, j;

    // escreve número no buffer
    nchar = sprintf(buf, "%d", n);
    // Quantos dígitos preciso escrever para a primeira classe?
    i = nchar % 3; if (!i) i = 3;
    // escrever a primeira classe
    fprintf(stream, "%.*s", i, ptr);
    // avançar
    ptr += i;
    // enquanto ainda houver classes para escrever,
    // escreva um ponto e depois a classe
    while (*ptr) {
        fputc('.', stream);
        fprintf(stream, "%.3s", ptr);
        // não esqueça de avançar o ponteiro
        ptr += 3;
    }
}

Then just put the call to this function in place of printf() :

void sql_rows(MYSQL_ROW sql) {
    puts("a quantidade de rows é ");
    escrever_numero(sql->row[0], stdout);
    puts("\n");
}

Note that the function I wrote only works with nonnegative integers, but this is for the specific case of counting lines (or anything else). If you need a function that treats negative numbers, just treat the possibility of a minus sign before the first class.

Finally, you can also override this FILE * parameter with a char * and switch calls to fprintf() with sprintf() if you want, but be careful to size your arrays correctly and also end the string with a FILE * , which does not have to do with %code% .

    
03.04.2017 / 22:18