Search code examples
iphoneobjective-cparsingdoublenslog

Objective C - Scientific Notation "String" into Double


I am reading in a text file with tons of values in this format: 1.36E-08

Is there a way to "parse" (i think thats the word i need) this value into a double? If I assign it straight forward it gives me 0.

//this NSLogs out "0.00000"
double x = 1.36E-08;

Solution

  • I think You want this:

    NSDecimalNumber *decNumber = [NSDecimalNumber decimalNumberWithString:@"1.36e-08"];
    
    NSLog(@"Number -> %g", [decNumber doubleValue]);
    

    Output:

    Number -> 1.36e-08