C: Any way to convert text string to float, i.e. 1e100? -


unfortunately in log have there strings notation (1.00e4), know can use printf "%e" specifier create notation, how read it?

unfortunately strtof , scanf seem return 0.00000 on "1e100", need write own parser this?

(updated)

what have input:
string "4.1234567e5", or "4.5e5"

desired output:
float 412345.67, or integer "450000"

start code:

#include <stdio.h> int main (void) {     double val;     double val2 = 1e100;     sscanf ("1e100", "%lf", &val);  // that's letter ell, not number wun !     printf ("%lf\n", val);          // that.     printf ("%lf\n", val2);         // , that.     return 0; } 

it outputs:

10000000000000000159028911097599180468360810000000000... 10000000000000000159028911097599180468360810000000000... 

the reason it's not 1100 because of nature of ieee754 floating point values. changing to:

#include <stdio.h>  int main (int argc, char *argv[]) {     double val;     int i;     (i = 1; < argc; i++) {         sscanf (argv[i], "%lf", &val);         printf ("   '%s' -> %e\n", argv[i], val);     }     return 0; } 

and running sample arguments gives:

pax$ ./qq 4.1234567e5 4.5e5 3.47e10 3.47e-10    '4.1234567e5' -> 4.123457e+05    '4.5e5' -> 4.500000e+05    '3.47e10' -> 3.470000e+10    '3.47e-10' -> 3.470000e-10 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -