hello can you help me please,my problem is that i don't know how to format numbers when the user input this 1500 it should display 1,500.00 and having total paid 3,080.00
please help me hoping for your positive responds...thank you in advance...
hello can you help me please,my problem is that i don't know how to format numbers when the user input this 1500 it should display 1,500.00 and having total paid 3,080.00
please help me hoping for your positive responds...thank you in advance...
There is no function to do this kind of numeric formatting, but one can be written. The hardest part is the group separation with commas.
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <locale.h>
#include <ctype.h>
typedef enum { FALSE, TRUE } BOOL;
BOOL format_numeric(char const* num, char* buf, int buf_sz) {
int num_x = strlen(num)-1, buf_x = 0;
int group_sz = 0; // current group size
struct lconv* lc;
char* group; // current group maximum size
setlocale(LC_NUMERIC, "");
lc = localeconv();
group = lc->grouping;
buf[buf_x] = '\0'; // make strcat() work
while (num_x >= 0) {
if (!isdigit(num[num_x]) || buf_x >= buf_sz-1)
return FALSE;
if (*group != CHAR_MAX && group_sz++ == *group) {
strcat(buf, lc->thousands_sep);
buf_x += strlen(lc->thousands_sep);
if (*group != lc->grouping && *(group+1) != 0)
++group;
group_sz = 0;
}
else {
buf[buf_x++] = num[num_x--];
buf[buf_x] = '\0'; // make strcat() work
}
}
strrev(buf);
return TRUE;
}
void main() {
char buf[20+1];
if (format_numeric("1000000000", buf, sizeof buf))
printf("|%s|\n", buf);
else
fprintf(stderr, "conversion error\n");
}
Currency is even harder, but it seems as if you are only appending ".00" to the user input instead of interpreting an assumed decimal: "1523" with an assumed decimal becomes "15.23".
There is no function to do this kind of numeric formatting, but one can be written. The hardest part is the group separation with commas.
#include <stdio.h> #include <string.h> #include <limits.h> #include <locale.h> #include <ctype.h> typedef enum { FALSE, TRUE } BOOL; BOOL format_numeric(char const* num, char* buf, int buf_sz) { int num_x = strlen(num)-1, buf_x = 0; int group_sz = 0; // current group size struct lconv* lc; char* group; // current group maximum size setlocale(LC_NUMERIC, ""); lc = localeconv(); group = lc->grouping; buf[buf_x] = '\0'; // make strcat() work while (num_x >= 0) { if (!isdigit(num[num_x]) || buf_x >= buf_sz-1) return FALSE; if (*group != CHAR_MAX && group_sz++ == *group) { strcat(buf, lc->thousands_sep); buf_x += strlen(lc->thousands_sep); if (*group != lc->grouping && *(group+1) != 0) ++group; group_sz = 0; } else { buf[buf_x++] = num[num_x--]; buf[buf_x] = '\0'; // make strcat() work } } strrev(buf); return TRUE; } void main() { char buf[20+1]; if (format_numeric("1000000000", buf, sizeof buf)) printf("|%s|\n", buf); else fprintf(stderr, "conversion error\n"); }
Currency is even harder, but it seems as if you are only appending ".00" to the user input instead of interpreting an assumed decimal: "1523" with an assumed decimal becomes "15.23".
hello sir thank you for the reply and putting some comments,since im just a newbie in C i will try to understand your code sir...more power to you...
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.