you can use snprintf to print a double value into a char array, then from end to head, replace '0' with '\ 0', finally you will get a number without zero zeros. Here is my simple code.
#include <stdio.h> #include <stdlib.h> #include <string.h> void remove_zeroes(double number, char * result, int buf_len) { char * pos; int len; snprintf(result, buf_len, "%lf", number); len = strlen(result); pos = result + len - 1; #if 0 /* according to Jon Cage suggestion, removing this part */ while(*div != '.') div++; #endif while(*pos == '0') *pos-- = '\0'; if(*pos == '.') *pos = '\0'; } int main(void) { double a; char test[81]; a = 10.1; printf("before it is %lf\n", a); remove_zeroes(a, test, 81); printf("after it is %s\n", test); a = 100; printf("before it is %lf\n", a); remove_zeroes(a, test, 81); printf("after it is %s\n", test); return 0; }
and the way out is
before it is 10.100000 after it is 10.1 before it is 100.000000 after it is 100
MYMNeo
source share