parsing NSString to Double

前端 未结 1 1812
孤城傲影
孤城傲影 2020-12-09 14:31

so I want to convert NSString to double. I found the following example:

NSString * s = @\"1.5e5\";
NSLog(@\"%lf\", [s doubleValue]);


        
相关标签:
1条回答
  • 2020-12-09 14:36

    You can use the NSScanner class:

    NSString *s = @"1.5e5";
    NSScanner *scanner = [NSScanner scannerWithString:s];
    double d;
    BOOL success = [scanner scanDouble:&d];
    

    If you want to ensure that the entire string has been scanned (no extra characters after the number), use

    BOOL isAtEnd = [scanner isAtEnd];
    
    0 讨论(0)
提交回复
热议问题