int main (void) { int quantite [] = { 1, 4, 3, 1, 1, 2, 0 }; char * reference [] = { "ABC", "DEF", "GHI", "JKL", "MNO", "PQR", NULL }; double prix [] = { 1500, 2040, 560, 2500, 38400, 125, 0 }; int i; char format [80]; double total = 0.0; setlocale (LC_ALL, ""); for (i = 0; reference [i] != NULL; i ++) { strfmon (format, 80, "%%5s : %#5n x %%d = %#5n\n", prix [i], prix [i] * quantite [i]); fprintf (stdout, format, reference [i], quantite [i]); total += prix [i] * quantite [i]; } strfmon (format, 80, " Total = %#5n\n", total); fprintf (stdout, format); return (0); }
void test_empty_precision (char *s, size_t m, double d) { strfmon (s, m, "%#.5n", d); /* { dg-warning "20: empty left precision in gnu_strfmon format" } */ /* { dg-begin-multiline-output "" } strfmon (s, m, "%#.5n", d); ^ { dg-end-multiline-output "" } */ strfmon (s, m, "%#5.n", d); /* { dg-warning "22: empty precision in gnu_strfmon format" } */ /* { dg-begin-multiline-output "" } strfmon (s, m, "%#5.n", d); ^ { dg-end-multiline-output "" } */ }
static void get_locale_dependent_values (struct locale_dependent_values *result) { strfmon (result->monetary, sizeof (result->monetary), "%n", 123.75); /* result->monetary is usually "$123.75" */ snprintf (result->numeric, sizeof (result->numeric), "%g", 3.5); /* result->numeric is usually "3,5" */ strcpy (result->time, nl_langinfo (MON_1)); /* result->time is usually "janvier" */ }
int main (int argc, char * argv[]) { double d; char buffer[80]; setlocale(LC_ALL, ""); if ((argc != 3) || (sscanf(argv[2], "%lf", & d) != 1)) { fprintf(stderr, "%s format valeur \n", argv[0]); exit(EXIT_FAILURE); } if (strfmon(buffer, 80, argv[1], d) >0) fprintf(stdout, "%s\n", buffer); return EXIT_SUCCESS; }
/* * Formats numbers in money format using locale parameters and moneyformat codes */ gint rlib_format_money(rlib *r, gchar **dest,const gchar *moneyformat, gint64 x) { gint result; #ifdef HAVE_STRFMON gdouble d; #endif *dest = g_malloc(MAXSTRLEN); #ifndef HAVE_STRFMON result = 0; (*dest)[0] = 0; #else d = ((gdouble) x) / RLIB_DECIMAL_PRECISION; result = strfmon(*dest, MAXSTRLEN - 1, moneyformat, d); #endif change_radix_character(r, *dest); return (result >= 0)? strlen(*dest) : 0; }
static int do_test (void) { int res = 0; for (int i = 0; i < ntests; ++i) { char buf[500]; if (setlocale (LC_ALL, tests[i].locale) == NULL) { printf ("failed to set locale %s\n", tests[i].locale); res = 1; continue; } strfmon (buf, sizeof (buf), "|%n|%!n|", -12.34, -12.34); int fail = strcmp (buf, tests[i].expected) != 0; printf ("%s%s\n", buf, fail ? " *** FAIL ***" : ""); res |= fail; } return res; }
void printamount(double amount,int flag) { char* amountStr; int chk=0; amountStr = (char*) malloc (25); if(amount > 10000000) { amountStr = " ?,???,???.?? "; chk=1; } else if (amount < -10000000) { strcat(amountStr,"("); strcat(amountStr,"?,???,???.??"); strcat(amountStr,")"); } else { strfmon(amountStr, 24, "%!(14#7.2n", amount); } if(flag==0) { fprintf(stdout,"| %s ",amountStr); } else { fprintf(stdout,"| %s |\n",amountStr); } if(chk==1) { if(flag==1) { fprintf(stderr,"ERROR: Cannot compute further due to overflow of balance\n"); exit(1); } } free(amountStr); }