Question:
character and float?
jcol
2006-09-29 09:34:17 UTC
char A;
float B=1.2;
how can i make char A take the value of 1.2?

i tried A=char(B) in my program....cant work

i also tried float(A)=B.... still wrong...
Seven answers:
holden
2006-09-29 09:38:05 UTC
there is know way to assign 1.2 to a single char. if you want a sequence of char's to hold a string containing the characters "1.2" you could:



char A[10];

sprintf(A,"%4.1f",B);
anonymous
2006-09-29 16:37:31 UTC
Casting, i.e. A = (char) B should work. Or you could convert it using ftoa. Why would you want to do this in the first place?
shivraj s
2006-09-29 16:47:08 UTC
very simple.. lol

use this c library function.



Answer: gcvt(B,2,A)



char * gcvt ( double value, int num, char * buffer );



Parameters.

value

Floating point value to be converted to a string.

num

Number of digits to generate.

buffer

Memory block where to store the resulting string.
PJ
2006-09-29 16:36:51 UTC
You can't. A character can only hold integer values from -128 to 127 (or from 0 to 255 if it's unsigned).
anonymous
2006-09-29 16:36:07 UTC
You should probably say what language you're using...
Nestor
2006-09-29 16:45:57 UTC
It will be helpfull if you tell what lang it is?
jrichard377
2006-09-29 16:36:43 UTC
If C, try sprintf()


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...