I have this code
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
  struct timeval current_time;
  gettimeofday(¤t_time, NULL);
  printf("seconds : %ld\nmicro seconds : %ld\n",
  current_time.tv_sec, current_time.tv_usec);
  char timestamp[100];
  sprintf(timestamp,"%ld.%ld",current_time.tv_sec,current_time.tv_usec);
  printf("timestamp in char:  %s\n", timestamp);
  float timestamp_f;
  timestamp_f = strtold(timestamp,NULL);
  printf("timestamp in float: %10.6f\n",timestamp_f);
  return 0;
}
That returns:
seconds : 1615776760
micro seconds : 957945
timestamp in char:  1615776760.957945
timestamp in float: 1615776768.000000
What happens to the decimals + the last digit is 8 instead of 0?
I used strtof instead, but still got somewhat similar results:
seconds : 1615776930
micro seconds : 767048
timestamp in char:  1615776930.767048
timestamp in float: 1615776896.000000
Thanks.
float. Don't usefloat. Usedouble.timestamp_fto adoubleand the format would be%lfinstead of%10.6f."%ld.%ld"-->"%ld.%06ld"%10.6fremains OK.lmakes no functional difference in printing.strtofas float only has 7 significant digits. You need to usestrtoldbut that functio nreturns along doubleso you need to assign it to a variable of that type. Changefloat timestamp_f;tolong double timestamp_f;and make sure your printf specifier is%10.6Lfso that printf knows it is a long double. godbolt.org/z/11Tqqo