While working on a project with the Arduino, I was frustrated by the lack of double-to-string functionality. I scoured the web and only found "dumb implementations" that only worked to a fixed precision and did not support larger ranges (outside the (long/int) boundaries).

So I've written one myself. I haven't had any problems with it, but if anyone notices any bugs it'd be greatly appreciated. Hopefully this will help others in the future , would've saved me quite some time. And also hope I put this in the right section :/ . Let me know what you guys think!

This will use "E+" or "E-" when the numbers start going out of range, and will shift accordingly to get the best precision (it's not "125333.00" and "0.32" and "235.30", it's just "12533" and "0.32" and "235.3").

The dtostre() function converts the double value passed in val into an ASCII representation that will be stored under s. The caller is responsible for providing sufficient storage in s.

Conversion is done in the format "[-]d.ddde?dd" where there is one digit before the decimal-point character and the number of digits after it is equal to the precision prec; if the precision is zero, no decimal-point character appears. If flags has the DTOSTRE_UPPERCASE bit set, the letter 'E' (rather than 'e' ) will be used to introduce the exponent. The exponent always contains two digits; if the value is zero, the exponent is "00".

If flags has the DTOSTRE_ALWAYS_SIGN bit set, a space character will be placed into the leading position for positive numbers.

If flags has the DTOSTRE_PLUS_SIGN bit set, a plus sign will be used instead of a space character in this case.

The dtostre() function returns the pointer to the converted string s.

char* dtostrf ( double __val, char __width, 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.

The dtostrf() function returns the pointer to the converted string s.

So, you should be able to vary the format of the string returned by dtostre(), but it seems more likely that you want to use dtostrf().

As far as I understand dtostrf() doesn't use "E" when numbers get too big.dtostre() has a fixed precision after the decimal point (specified clearly in the documentation). So you're forced to have stuff like 1.00000E6 if you want to make sure you cover everything (my version will give 1E6 and 2.345E8 and 2.34567E4; expands and shrinks as necessary).

So those aren't viable replacements for my use. dtostre() (essentially scientific notation) isn't bad for most uses, but sometimes (at least the project I'm working on) you don't want stuff like 6.00000E0 to display 6. You only want the scientific notation for big or small numbers, and as I said I also don't want the extra 0s if they aren't necessary.