How do I convert a float into char*?
There is a function in the standard Arduino library called dtostrf()
. I think of it as "Decimal to String Float". You pass in the float, how wide you want the whole number to be (if it will fit), the number of decimals of precision - and the buffer you want it to fill.
Note that! You need to provide the buffer, and you need to be careful to provide more than enough! Don't forget to add 1, too (to store the NUL character at the end):
char result[8]; // Buffer big enough for 7-character float
dtostrf(resistance, 6, 2, result); // Leave room for too large numbers!
This will give result
values like " 1.23"
and " -1.23"
and "123456789"
(without the quotes). Note the last example - it won't truncate the number if it's too large, and that 10- character result
(don't forget the final NUL
) just overflowed your buffer...
Incidentally, if you specify a negative width, it won't right-justify the answer, it'll left-justify it (put any spaces on the end instead of the beginning).