Pergunta

I have two strings which hold values say for ex:35.5044752 97.3955550

Let me convert it :

    double f1=[la doubleValue];
    double f2=[lo doubleValue];

(value of f1 and f2 is dynamic say for example f1= "35.5044752" f2="97.3955550" )

if i want to print it in NSLog i will do as follows :

 NSLog(@" %f  %f ",f1,f2); 

And it returns 35.504475 97.395555

hence i change it as

NSLog(@" %0.7f  %0.7f ",f1,f2);

And gets the full values like 35.5044752 97.3955550

Now i need it to use in the Coordinate like below:

annotation.coordinate=CLLocationCoordinate2DMake(coord.longitude, coord.longitude);

My Question is how can i implement %0.7f here like which i made in NSlog ?

so that i should take input fully instead of reducing or altering the value.

Foi útil?

Solução

make a try like this. Directly pass values to obj center

CLLocationCoordinate2D center;
...
else if ([elementName isEqualToString:@"Lat"]) {
    center.latitude = [[attributeDict objectForKey:@"degrees"] doubleValue];
}
else if ([elementName isEqualToString:@"Lon"]) {
    center.longitude = [[attributeDict objectForKey:@"degrees"] doubleValue];
}
...

OR

Archived the coordinate in foundLocation:

NSNumber *latitudeObject = [NSNumber numberWithDouble:coord.latitude];
NSNumber *longitudeObject = [NSNumber numberWithDouble:coord.longitude];
NSArray *coordinateArray = [NSArray arrayWithObjects:latitudeObject, longitudeObject, nil];

[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:coordinateArray] 
                                          forKey:WhereamiCoordinatePrefKey];

Unarchived the coordinate in viewDidLoad:

NSArray *coordinateArray = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] 
                                                                       objectForKey:WhereamiCoordinatePrefKey]];

CLLocationCoordinate2D savedCoordinate = CLLocationCoordinate2DMake([[coordinateArray objectAtIndex:0] doubleValue], 
                                                                    [[coordinateArray objectAtIndex:1] doubleValue]);

MKCoordinateRegion savedRegion = MKCoordinateRegionMakeWithDistance(savedCoordinate, 250, 250);
[worldView setRegion:savedRegion animated:YES];

Outras dicas

The %0.7f format specifier deals with how your value is displayed, not with how it is stored. A double is always double and has its inherent precision and nothing you can do, short of casting it to another data type, will change that.

As far as I know, the double data type offers the highest floating point precision of the standard data types. If you need greater precision than that, you're going to have to use something other than a double.

In other words, when you perform an operation on a double, it is always calculated to the full precision allowed by the double data type.

For more information on the subject, see the Wikipedia entry on floating point data types.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top