Game Development Community

From float to char* ?

by Xavier "eXoDuS" Amado · in Torque Game Engine · 01/21/2002 (9:59 am) · 8 replies

I was looking for a way to convert a float variable (for example player's health) to a char* to be able to print it in a guiCtrl using renderJustifiedText() function, which only accepts a char* as its text parameter. Or maybe there's another function to render float numbers? I couldnt find it tho.
Please any help would be appreciated.
Thanks

#1
01/21/2002 (10:15 am)
Check out the 'sprintf' function. It should meet all your string formatting needs.
#2
01/21/2002 (10:43 am)
maybe you can give me an example of that kind of conversion? MSDN isnt any helpful :P
#3
01/21/2002 (10:47 am)
Here it is
float var = 32.467;
char buff[256];
sprintf( buff, "%.2f", var );  // 2 decimals after .
#4
01/21/2002 (11:56 am)
i figured the command out before you posted that .. sorry, but maybe it comes in handy anyway, what do you mean 2 decimal points afterwards?
What if i would like to eliminate the decimal part completely?
#5
01/21/2002 (12:05 pm)
I mean :
var = 32.76
%.2f -> 32.76
%.0f -> 32
#6
01/21/2002 (2:25 pm)
nice :)
thanks a lot
#7
01/22/2002 (2:09 pm)
you should really be using dSprintf instead of sprintf, as that will ensure platform compatibility
#8
01/22/2002 (5:25 pm)
yes i'm using dSprintf now.. i figured it out later :)