开发者

XML value (to NSString) to float

开发者 https://www.devze.com 2023-02-16 06:47 出处:网络
I\'m parsing an xml file using TBXML. One of the value is a latitude coordinate (e.g. 45.503508). I need to read that and converting into a float vari开发者_如何学编程able.

I'm parsing an xml file using TBXML. One of the value is a latitude coordinate (e.g. 45.503508). I need to read that and converting into a float vari开发者_如何学编程able.

I'm doing this

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);

what's the problem? If my xml value is 45.503508, the value of "myfloat" is 45.000000! Every time!

What is wrong?


The problem seems to be that there's a comma (instead of a period) in the string returned from the XML element.

As such, you should be able to swap this out with a period using the NSString stringByReplacingOccurrencesOfString:withString: method and then use the NSString doubleValue method to extract the figure you require.

0

精彩评论

暂无评论...
验证码 换一张
取 消