Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,038 questions

40,849 answers

573 users

How to convert double to string in C

2 Answers

0 votes
#include <stdio.h>
 
int main(void)
{
    double d = 23445.938076;
    char str[16] = "";
     
    sprintf(str, "%.6lf", d);
 
    printf("%s", str);
     
    return 0;
}
  
   
   
   
/*
run:
   
23445.938076
 
*/

 





answered Oct 2, 2023 by avibootz
edited Nov 12, 2023 by avibootz
0 votes
#include <stdio.h>

void double_to_string(char buf[], int size, double d) {
    for (int i = 0; i < size; i++) {
        double tmp;

        // int snprintf(char *str, size_t size, const char *format, …);
        if (snprintf(buf, size, "%.*g", i, d) >= size) {
            break;
        }

        sscanf(buf, "%lf", &tmp);
        
        if (tmp == d) {
            break;
        }
    }
}


int main() {
    char str[16] = "";
    double d = 23445.938076;
    
    double_to_string(str, 16, d);
    
    puts(str);

    return 0;
}




/*
run:

23445.938076

*/

 





answered Nov 12, 2023 by avibootz

Related questions

1 answer 47 views
1 answer 59 views
1 answer 61 views
61 views asked Jun 20, 2021 by avibootz
1 answer 65 views
...