-1

我有两个包含值的字符串,例如:35.5044752 97.3955550

让我转换它:

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

(f1 和 f2 的值是动态的,例如 f1= "35.5044752" f2="97.3955550" )

如果我想在 NSLog 中打印它,我将执行以下操作:

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

它返回 35.504475 97.395555

因此我将其更改为

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

并获得完整的值,如 35.5044752 97.3955550

现在我需要它在坐标中使用,如下所示:

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

我的问题是如何像我在 NSlog 中那样实现 %0.7f ?

所以我应该完全接受输入,而不是减少或改变价值。

4

2 回答 2

1

试试这样。直接传值给objcenter

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

或者

存档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];

取消归档 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];
于 2013-10-30T08:19:04.873 回答
0

%0.7f格式说明符处理值的显示方式,而不是存储方式。Adouble总是double并且具有其固有的精度,除了将其转换为另一种数据类型之外,您无能为力,将改变这一点。

据我所知,该double数据类型提供了标准数据类型中最高的浮点精度。如果您需要比这更高的精度,您将不得不使用除double.

换句话说,当您对 a 执行操作时,它总是以数据类型double允许的完整精度计算。double

有关该主题的更多信息,请参阅Wikipedia entry on floating point data types

于 2013-10-30T07:33:24.267 回答