Hi All,
What's the easiest way to convert a number to a string (for example an integer?) in C/C++?
In the past Ive used itoa but I believe thats non-standard.
Thanks in advance - it's been a while since I've done any C/C++ and this one has complete escaped me - it's also missing from my books :/
Cheers,
S.
Convert number to a string
Either use itoa as you said, or do the dirty way with sprintf.
Code: Select all
int i = 5023;
char number[32];
snprintf(number,32,"%i",i);
Why is "sprintf" dirty...? Really, i'm new and I use this...?Raphael wrote:Either use itoa as you said, or do the dirty way with sprintf.
Code: Select all
int i = 5023; char number[32]; snprintf(number,32,"%i",i);
Code: Select all
char buffer[200];
float num = 20.39999999;
sprintf(buffer,"Why is this wrong...? Does this float look nice %.02...?",num);
ADe
P.S. Apart from declaring more bytes for the char than were needed... :D
Well, it's not really dirty, I just called it that in this case, as it needs the extra line to declare your buffer ;)ADePSP wrote: Why is "sprintf" dirty...? Really, i'm new and I use this...?
well, I think it should be %.02f but other than that it's perfectly fineADePSP wrote:If there is something wrong with that please tell us...?Code: Select all
char buffer[200]; float num = 20.39999999; sprintf(buffer,"Why is this wrong...? Does this float look nice %.02...?",num);