Search code examples
stringvisual-c++type-conversiondoublec++17

How can I convert any numbers, for example 822042461 (string-type) to 822042461 (double-type)?


How can I convert any numbers, for example 822042461 (string-type) to 822042461 (double-type) in C++ ?? Thanks a real lot !


Solution

  • sscanf may be useful:

    /* sscanf example */
    #include <stdio.h>
    
    int main ()
    {
      char sentence []="822042461";
      double i;
    
      sscanf (sentence,"%lf",&i);
      printf ("%lf\n",i);
    
      return 0;
    }
    
    $ gcc -o /tmp/test.exe  /tmp/test.cpp
    $ /tmp/test.exe                      
    822042461.000000