In said:
How can I convert a double such as 23.1 to a string?
Are you sure you can have the value 23.1 represented by a double in the
first place?
I don't want to use
any library functions because the code is being written in avr-gcc and I
don't like the way dtostrf() has been implemented. Thanks.
If you're dealing with reasonable values (i.e. you don't have to resort to
the exponential format), it's quite straightforward.
Deal with the sign first and make the value positive if it was negative.
If the number is greater or equal to 1, keep dividing it by 10 until it
no longer is and count the number of divisions. Now, perform the
following simple steps:
1. If the division count is zero, insert the decimal point in the string.
2. Decrement the division count.
3. Multiply the value by 10.
4. Convert the integer part of the result to a digit code and insert it
into the string.
5. The fractional part becomes the new value you're dealing with.
6. If the division count is equal to the negative of the number of desired
decimals, insert the null terminator and you're done.
7. Go to step 1.
It may not be the fastest or the most accurate algorithm, but it is the
easiest to implement with no library support.
Dan