Hello. I was wondering if there is a standard library function to convert a numeric value into a string.
Compiler: Turbo C++ v1.01
Hello. I was wondering if there is a standard library function to convert a numeric value into a string.
Compiler: Turbo C++ v1.01
sprintf( strVariable, "%d", intVariable );
will do it, but no doubt someone knows a more efficient way!
you could probably use the strcpy();
but you would have to use the sting.h header file
Hi!
I've never heard about such function, but snprintf() could be a good choice. I'm not sure if this code below is working properly, but try it. The integer parsing stuff functional, it will convert any integer to string.
char *ParseNum(void* num, int flag) {
#define I 1
#define D 2
char *string; int x = (num < 0) ? 1 : 0;
unsigned int div = 10, count;
size_t size = sizeof(char);
switch(flag) {
case I:
x = 0, count = (int) num;
while((count = count / div) > div) { x = x++; } x = x + 1;
string = (char *) xmalloc(x);
snprintf(string,(size * x) + 1,"%d",(int) num);
memset(&string[x+1],'\0',size); break;
case D:
string = (char *) xmalloc(sizeof(double)+1);
snprintf(string,(sizeof(char) * sizeof(double)) + 1,"1.5Lf",num);
break;
}
return string;
}
Hope this will be useful to you!
Bye!
Best Regards,
Bill
Just a comment:
The void* arg is not good, I've used for testing something. Use double instead.
Best Regards,
Bill
>>Just a comment:
.. and two more comments:
- use code tags when posting code;
- don't double post, if you've made a mistake in a post, just edit it.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]