Tenho um valor float = 1.5 e quero converter para char = "1.5", como fazer?
Em qual função vai usar este "char"?
Usas a funçao dtostrf
The dtostre() function returns the pointer to the converted string s.
char* dtostrf ( double __val,
signed char __width,
unsigned char __prec,
char * __s
)The dtostrf() function converts the double value passed in val into an ASCII representationthat will be stored under s. The caller is responsible for providing sufficient storage in s.
Conversion is done in the format "[-]d.ddd". The minimum field width of the output string (including the '.' and the possible sign for negative values) is given in width, and prec determines the number of digits after the decimal sign. width is signed value, negative for left adjustment.
The dtostrf() function returns the pointer to the converted string s.
float a = 23.6;
char data[5];
void setup()
{
Serial.begin(9600);
char *valor = dtostrf(a,4,2,data);
Serial.println(valor);
}
void loop()
{
}
FernandoGarcia:
Em qual função vai usar este "char"?
Tenho 2 displays de 7 segmentos e estava tentando separar os valores do float.
HugoPT:
Usas a funçao dtostrfThe dtostre() function returns the pointer to the converted string s.
char* dtostrf ( double __val,
signed char __width,
unsigned char __prec,
char * __s
)The dtostrf() function converts the double value passed in val into an ASCII representationthat will be stored under s. The caller is responsible for providing sufficient storage in s.
Conversion is done in the format "[-]d.ddd". The minimum field width of the output string (including the '.' and the possible sign for negative values) is given in width, and prec determines the number of digits after the decimal sign. width is signed value, negative for left adjustment.
The dtostrf() function returns the pointer to the converted string s.
float a = 23.6;
char data[5];
void setup()
{
Serial.begin(9600);
char *valor = dtostrf(a,4,2,data);
Serial.println(valor);
}
void loop()
{
}
Funcionou, muito obrigado.
Algum motivo para quereres fazer assim?
O método print, tanto quanto sei, faz essa conversão... apenas não controlas tanto os parâmetros da conversão.
bubulindo:
Algum motivo para quereres fazer assim?O método print, tanto quanto sei, faz essa conversão... apenas não controlas tanto os parâmetros da conversão.
Estou usando os displays de 7 segmentos com o 4511.
Eu recebo o valor em float(voltagem de uma pilha), converto pra char e separo os valores, converto pra binário e mostro nos displays, qual outro jeito eu poderia fazer?
Entendi da seguinte maneira
No caso da função:
dtostrf(a,4,2,data) os parâmetros seriam o seguinte:
a = valor a ser convertido
4 = número de caracteres (incluindo o ".") o char vai conter
2 = numero de casas decimais do novo char
Seria isto? Decupem a burrice!
vfnishiyama:
Entendi da seguinte maneiraNo caso da função:
dtostrf(a,4,2,data) os parâmetros seriam o seguinte:
a = valor a ser convertido
4 = número de caracteres (incluindo o ".") o char vai conter
2 = numero de casas decimais do novo char
Seria isto? Decupem a burrice!
Sim, é isso. Apenas não tenho a certeza do que quer dizer com o "novo char".