Output comma separation

How can I get a comma between the digits of the number specified in the input, for example 1000 10000 100 000 100 000 and it separates the digits of the number, for example 1000 10000 100000 1 000 000 as output

Is there any function (library) in C to create a program for it?

+1
source share
4 answers

Using the standard print flag and locale settings:

 #include <locale.h> #include <stdio.h> int main() { int value = 1234567; if (!setlocale(LC_ALL, "en_US.UTF-8")) { fprintf(stderr, "Locale not found.\n"); return 1; } printf("%'d\n", value); return 0; } 

But using x mod 3 and a Duff device , you can create your own (portable) function:

 #include <stdio.h> #include <stdlib.h> char *thousand_sep(long x) { char s[64], *p = s, *q, *r; int len; len = sprintf(p, "%ld", x); q = r = malloc(len + (len / 3) + 1); if (r == NULL) return NULL; if (*p == '-') { *q++ = *p++; len--; } switch (len % 3) { do { *q++ = ','; case 0: *q++ = *p++; case 2: *q++ = *p++; case 1: *q++ = *p++; } while (*p); } *q = '\0'; return r; } int main(void) { char *s = thousand_sep(1234567); printf("%s\n", s); free(s); return 0; } 

Output:

 1,234,567 

EDIT:

if i want to do the same in java then ??

Sorry, I don't know Java, maybe useful (in javascript using regex's):

 Number.prototype.thousand_sep = function(decs){ var n = this.toFixed(decs).toString().split('.'); n[0] = n[0].replace(/\B(?=(\d{3})+(?!\d))/g, ','); return n.join('.'); }; ... var x = 1234567; alert(x.thousand_sep(0)); 
0
source
  • To enter data, you just need to read integers separated by commas. I would use strtol() and skip the comma manually.
  • To output, add ' printf() to the corresponding output specifier, i.e. printf("%'d", 1000); should print an int 1000 value as 1,000 . It depends on your language, see the man page .
0
source

For java (as the OP asked in the comment somewhere), use:

 String formattedString = NumberFormat.getInstance().format(number); 

If you need a specific language:

 String formattedString = NumberFormat.getInstance(Locale.FRENCH).format(number); 

If you need other number formats (for currency, percent, etc.):

 NumberFormat.getCurrencyInstance().format(number); NumberFormat.getIntegerInstance().format(number); NumberFormat.getPercentInstance().format(number); 

[each of them may be assigned a language standard]

For more control over formatting options, you can switch to DecimalFormat :

 new DecimalFormat("0,000.00").format(number); 

I recommend that you study both classes (NumberFormat and DecimalFormat) to understand your possibilities.

0
source

I did not find libraries that could solve this problem.

I wrote code to perform the required operation.

 #include "stdio.h" #include "string.h" void func (int in, char * modified_int) { char my_int[1000]; sprintf (my_int, "%d", in); int len = strlen(my_int); int curr_index = len + len/3 - 1; modified_int[curr_index+1] = '\0'; int modulo_3 = 0; for (int i = len-1; i >= 0; i--, curr_index--, modulo_3++) { char abc = my_int[i]; modified_int[curr_index] = abc; if ((modulo_3 == 2) && (i != 0)) { curr_index--; modified_int[curr_index] = ','; modulo_3 = -1; } } } int main () { char my_int[1000]; int n = 1000; func(n, my_int); printf("%s\n", my_int); return 0; } 

Please let me know if this does not solve your problem.

0
source

Source: https://habr.com/ru/post/983047/


All Articles