What is the most efficient way to format strings with rounding and thousand separator in C? -


so have inner loop situation buffer of floating point , integer values copied on buffer in string format.

what alternatives round , insert thousand separator when formatting strings? whatever approach end using, has flexible enough in permitting different formats. also, because inner loop scenario, want optimize solution far possible.

it seem locale.h 1 way it. in case, how can setup customized locales, , how use them? or there better alternative altogether? if noob question please point me in right direction.

edit:

here few examples clarify:

1000 gives 1,000 (if want use , thousand separator)

1000 gives 1 000 (if want use space thousand separator)

1000.123 gives 1,000.1 (if want round 1 digit , use , thousand separator)

0 gives `` (if want show 0 blank string)

i on posix system btw...

you can try set locale using setlocale , use printf ' flag , precision value rounding. whether work, depends on c library.

see following program:

#include <locale.h> #include <stdio.h>  int main() {     double value = 1234567890.123;      if (!setlocale(lc_all, "en_us.utf8")) {         fprintf(stderr, "locale not found.\n");         return 1;     }      printf("%'.0f\n", value);     printf("%'.1f\n", value);      return 0; } 

on ubuntu system, output is:

1,234,567,890 1,234,567,890.1 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -