Use snprintf() from stdlib.h. Worked for me.

double num = 123412341234.123456789; 
char output[50];

snprintf(output, 50, "%f", num);

printf("%s", output);
Answer from sujeeth.mr on Stack Overflow
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ double to string and string to double
r/C_Programming on Reddit: Double to string and string to double
June 7, 2023 -

I'd like a pair of functions atof and ftoa. The atof function converts a string (char *) into a double. The ftoa function converts a double into a string (char *). The first and most obvious requirement is that atof(ftoa(x)) = x for all double-precision floating point values x including special values (ยฑ0, ยฑโˆž and nan). My second requirement is that ftoa generate the shortest possible representation for all inputs (including denormalized floats) such that the first condition is satisfied, e.g. it can choose between %f and %g for this purpose.

Does such a pair of functions exist and, if so, where can I download the C source code to them?

๐ŸŒ
All About Circuits
forum.allaboutcircuits.com โ€บ home โ€บ forums โ€บ embedded & programming โ€บ programming & languages
Convert double to string in C | All About Circuits
April 8, 2010 - Click to expand... Yes. I need to know the number of characters in the variable. 2.34 has 4 100.34 has 6 etc. ... There's no unique string representation of a given double, as there are lots of approximate choices. Examples: printf("%.3f", v), printf("%.8f, v), printf("%.4e", v).
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 38507-double-string-conversion.html
double to string conversion
Originally posted by penney Actually I believe that if you are converting a double then you should use: sprintf(str,"%lf", yourdouble); /* and whatever precision spec's you want */ %f is for floats and %lf for doubles. This is undefined behavior for pre-C99 implementations.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ c-program-for-double-to-string-conversion
C Program For Double to String Conversion - GeeksforGeeks
August 1, 2022 - We can use sprintf to add extra text (as required) to the string at the same time. ... // C Program to demonstrate // Double to String Conversion #include <stdio.h> int main() { double n = 456321.7651234; char str[100]; sprintf(str, "%f", n); ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ home โ€บ c_standard_library โ€บ c standard library strtod function
C Standard Library strtod Function
August 29, 2012 - Discover how to use the strtod function in C for converting strings to double with our detailed guide and examples.
๐ŸŒ
IncludeHelp
includehelp.com โ€บ code-examples โ€บ c-double-to-string-conversion.aspx
C - Double to String Conversion Code Example
#include <stdio.h> int main() { // Create a double variable and assign value double x = 12345.678901; // Create a string to store string value char result[32]; // Using sprintf() to convert double to string sprintf(result, "%f", x); // Printing the value printf("Double valuue (x) : %f\n", x); ...
Find elsewhere
๐ŸŒ
Quora
quora.com โ€บ Can-we-convert-double-to-string-in-C
Can we convert double to string in C++? - Quora
Get started for free today! ... A double can be converted into a string in C++ using std::to_string.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-do-i-convert-a-double-into-a-string-in-cplusplus
How do I convert a double into a string in C++?
In this approach, we have used the std::to_string() function for converting double into a string.
๐ŸŒ
Raspberry Pi Forums
forums.raspberrypi.com โ€บ board index โ€บ hardware and peripherals โ€บ raspberry pi pico โ€บ sdk
How to convert a double to a C string? - Raspberry Pi Forums
set_pico_float_implentation(my_app none) in your CMakeLists.txt, as the printf implementation for floats uses the floating point code! ... Thanks everybody! sprintf() indeed works with doubles and it solved by problem. However, I am having problems with formatting int64 values.
๐ŸŒ
TechOnTheNet
techonthenet.com โ€บ c_language โ€บ standard_library_functions โ€บ stdlib_h โ€บ strtod.php
C Language: strtod function (Convert String to Double)
If the strtod function converts a value that is too large or too small to convert, it will store ERANGE in errono. In the C Language, the required header for the strtod function is: ... /* Example using strtod by TechOnTheNet.com */ #include <stdio.h> #include <stdlib.h> #include <string.h> ...
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ faster way to convert double to string, not using "%f"?
r/C_Programming on Reddit: Faster way to convert double to string, not using "%f"?
August 22, 2022 -

Have to output millions of doubles to text files, so wondering if there is a faster way to convert double => text, besides using %f in one of many settings printf, sprintf, others I could probably find.

In the way that you could convert an int to text by using modulo and division to get the decimal digits. I assume that's faster than running through %d.

EDIT: I finally settled on %d and multiplying the double values by 1000000, converting to int, then converting back on the receiving end. More than enough precision for my specific application, not really elegant, but the time saving is significant. The double->int and reverse conversions are just a few instructions, much faster than converting the double to a string and back to double.

๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ api โ€บ system.double.tostring
Double.ToString Method (System) | Microsoft Learn
... The string representation of the value of this instance. The following example uses the default Double.ToString() method to display the string representations of a number of Double values.
๐ŸŒ
IBM
community.ibm.com โ€บ community โ€บ user โ€บ viewdocument โ€บ double-to-string-in-c
C/C++ - IBM TechXchange Community
Your one-stop destination to learn and collaborate about the latest innovations in IBM C/C++ for z/OS compilers to develop high-performing C/C++ applications and system programs on z/OS while maximizing hardware use and improving application performance ยท In C++ , there are multiple functions ...
๐ŸŒ
Cplusplus
cplusplus.com โ€บ reference โ€บ string โ€บ to_string
std::to_string - string
string to_string (int val);string ... val);string to_string (double val);string to_string (long double val); ... Returns a string with the representation of val. The format used is the same that printf would print for the corresponding type:...