Iphone 要浮动的XML值(到NSString)
我正在使用TBXML解析一个xml文件。 其中一个值是纬度坐标(例如45.503508)。 我需要读取它并将其转换为浮点变量 我正在这么做Iphone 要浮动的XML值(到NSString),iphone,xml,nsstring,floating-point,Iphone,Xml,Nsstring,Floating Point,我正在使用TBXML解析一个xml文件。 其中一个值是纬度坐标(例如45.503508)。 我需要读取它并将其转换为浮点变量 我正在这么做 TBXMLElement *loc_latitudine = [TBXML childElementNamed:@"latitude" parentElement:loc_location]; //read the xml attribute NSString *string = [NSString stringWithFormat:@"%@", [TBXM
TBXMLElement *loc_latitudine = [TBXML childElementNamed:@"latitude" parentElement:loc_location]; //read the xml attribute
NSString *string = [NSString stringWithFormat:@"%@", [TBXML textForElement:loc_latitudine]];
float myfloat = [string floatValue];
NSLog(@"%f", myfloat);
有什么问题吗?
如果我的xml值是45.503508,“myfloat”的值是45.000000!
每一次
怎么了?问题似乎是XML元素返回的字符串中有一个逗号(而不是句点)
因此,您应该能够使用
stringByReplacingOccurrencesOfString:withString:
方法将其替换为一个句点,然后使用NSStringdoubleValue
方法提取所需的图形。NSLog(@“string:%@”,string);是“45503508”。使用[string doubleValue]
(与floatValue相反)有什么区别吗?没有人:(我试图将字符串中的“.”替换为“.”,但什么都没有!啊-没有注意到逗号。您如何尝试替换它?(可能是NSStringstringByReplacingOccurrencesOfString:withString:
方法?)是的,是的,我试过了…但是没有改变!!