www.digitalmars.com         C & C++   DMDScript  

c++ - Converting float to char

reply Elisha Kendagor <kendagor iconnect.co.ke> writes:
I have a real number....
float r
and I want to store it into
char floatstr[6]
How do I get to do it?

Or: Do you know how I can display a real number using DrawText, TextOut,
SetDlgItemText.... etc?

Please reply soon!
Apr 06 2001
next sibling parent reply Jan Knepper <jan digitalmars.com> writes:
I would use double instead of float.


Use: double  d;
      TCHAR   str [ 32 ];

      _stprintf ( str, "%.2f", d );

Or check into the ecvt, fcvt, gcvt function in stdlib.h

Don't worry, be Kneppie!
Jan



Elisha Kendagor wrote:

 I have a real number....
 float r
 and I want to store it into
 char floatstr[6]
 How do I get to do it?
 
 Or: Do you know how I can display a real number using DrawText, TextOut,
 SetDlgItemText.... etc?
 
 Please reply soon!

Apr 06 2001
parent Elisha Kendagor <kendagor iconnect.co.ke> writes:
Thanks !! It really Knepped it.

Jan Knepper wrote:

 I would use double instead of float.

 Use: double  d;
       TCHAR   str [ 32 ];

       _stprintf ( str, "%.2f", d );

 Or check into the ecvt, fcvt, gcvt function in stdlib.h

 Don't worry, be Kneppie!
 Jan

 Elisha Kendagor wrote:

 I have a real number....
 float r
 and I want to store it into
 char floatstr[6]
 How do I get to do it?

 Or: Do you know how I can display a real number using DrawText, TextOut,
 SetDlgItemText.... etc?

 Please reply soon!


Apr 06 2001
prev sibling parent Roland <rv ronetech.com> writes:
below a way to make a string with a double

char* str_double(char* s, const double d, const int prec, const char mod) {
//prec=nb chiffres apres le . decimal, 0 -> 6
//mod='f,'e/E','g/G'
//f -> format decimal: [-]dd.dddd
//e/E -> format scientifique: [-]d.dddd e/E+-dd
//g/G -> f si precision suffisante, e/E sinon
//return s
 char format[16];
 strcpy(format,"%-.");
 format[3]=(prec==0) ? '6' : '0'+prec;
 format[4]=mod;
 format[5]=0;
 sprintf(s,format,d);
 return s;
}

Roland

Elisha Kendagor a écrit :

 I have a real number....
 float r
 and I want to store it into
 char floatstr[6]
 How do I get to do it?

 Or: Do you know how I can display a real number using DrawText, TextOut,
 SetDlgItemText.... etc?

 Please reply soon!

May 02 2001