ComponentsSeparatedByString returns an invalid result

I used this code to cut a string

NSString *titleString = @"22.225453615805794,113.554006577014889"; NSArray *array = [titleString componentsSeparatedByString:@","]; NSLog(@"title string %@", titleString); NSLog(@"first %.15f", [[array objectAtIndex:0] floatValue]); NSLog(@"second %.15f", [[array objectAtIndex:1] floatValue]); 

but why does he return

+22.225454330444336 as well as +113.554008483886719

+4
source share
2 answers

Since floating point numbers are not so accurate, you can get higher precision by calling doubleValue instead of floatValue :

 NSLog(@"second %.15f", [[array objectAtIndex:1] doubleValue]); 

This is not a problem with componentsSeparatedByString:

+7
source

I think there is a problem with converting a string to a float. Try using double.

+1
source

Source: https://habr.com/ru/post/1401542/


All Articles