简体   繁体   English

将数组字符串对象转换为浮点数

[英]Convert array string objects to floats

Longtime lurker (but still majorly sucky programmer) here. 长期潜伏在这里(但仍然主要是程序员)。 I've looked around for the answer to this and couldn't find any. 我到处寻找答案,但找不到任何答案。

I'm trying to read an array from a plist, and then turn those objects from strings to floats. 我试图从plist读取数组,然后将那些对象从字符串转换为浮点型。

The code that I'm trying right now declares an NSNumberFormatter , and tries to read in and convert to a float there. 我现在正在尝试的代码声明一个NSNumberFormatter ,并尝试读入并转换为其中的float。 It's not working, the NSLog always shows a 0 for those values. 它不起作用, NSLog的这些值始终显示为0。

Here's my code that I'm using to (successfully) read-in the array from Plist , and (not successfully) convert its strings to floats: 这是我用来(成功地)从Plist读取数组并将(但不成功地)将其字符串转换为浮点数的代码:

//Find the Plist and read in the array:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [paths objectAtIndex:0];
NSString *filePath =  [docDirectory stringByAppendingPathComponent:kFilename];
//Working up til here, the NSLog correctly shows the values of the array
NSArray *fileArray = [[NSArray alloc] initWithContentsOfFile:filePath];

//Turn object in array from string to float
NSNumberFormatter *format = [[NSNumberFormatter alloc] init];
[format setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *readinVol = [format numberFromString: fileArray [0]]; 
//This line not working, I believe- intended to read-in as an NSNumber, 
//and then convert to float below:
CGFloat readVol = [readinVol floatValue] * _volFloat;

So my question is: 所以我的问题是:

How can I turn my objects stored in the array from their current strings into more usable floats? 如何将数组中存储的对象从当前字符串转换为更可用的浮点数? I'd ideally like to do this all in one line with a loop, but would also be happy setting up separate CGFloats for each (such as readVol ). 理想情况下,我希望通过循环在一行中完成所有这些操作,但是也很乐意为每个设置单独的CGFloats (例如readVol )。

Thanks in advance for any help. 在此先感谢您的帮助。

The problem with NSNumberFormatterDecimalStyle might be that it is locale dependent. NSNumberFormatterDecimalStyle的问题可能是它与语言环境有关。 For example, with my German locale, the number 1.234,56 is converted correctly, but 1234.56 cannot be converted. 例如,在我的德语语言环境中,数字1.234,56正确转换,但是1234.56无法转换。 So you could set a defined locale to solve the problem: 因此,您可以设置一个定义的语言环境来解决该问题:

NSNumberFormatter *format = [[NSNumberFormatter alloc] init];
[format setNumberStyle:NSNumberFormatterDecimalStyle];
[format setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];

Alternatively, NSString has a floatValue method that gives the contents of the string as a float : 另外, NSString有一个floatValue方法,该方法将字符串的内容作为float

float f = [fileArray[0] floatValue];

Since CGFloat can be float or double , depending on the architecture, you may want to use doubleValue to be on the safe side: 由于CGFloat可以是floatdouble ,因此取决于体系结构,您可能需要使用doubleValue安全的:

CGFloat f = [fileArray[0] doubleValue];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM