iOS数组字符串,格式为00:00:00到数组浮点数,格式为00.00.00。目标C.

时间:2017-01-19 10:48:00

标签: ios objective-c floating-point nsarray

我正在寻找如何将Array字符串转换为Array float,数组字符串格式为00:00:00(hh:mm:ss),数组float为00.00.00原因是因为数组浮点数是计算的在图表中现在我只有这个

example array
   TimeRunning = @[@"01:10:50", @"01:03:23", @"03:04:22"];//arrayStrig

真实代码是

timeRunning = [obj valueForKey:@"oficialTime"];
NSLog(@"%@",timeRunning); //out 01:02:03 10:10:02 10:38:59

float ofcTime =[[obj valueForKey:@"oficialTime"]floatValue];
NSLog(@"float : %f",ofcTime); //out 1.000000 10.000000 10.000000

我试试这个

NSString * dataString = [TimeRunningString stringByReplacingOccurrencesOfString:@":" withString:@"."];
NSLog(@"??????: %@",dataString); //out 01.02.03  10.10.02  10.38.59

NSArray *listItems = [dataString componentsSeparatedByString:@"."];
NSLog(@"Real: %@",listItems);//out  10,
                                    10,
                                    02

NSString *covert = [NSString stringWithFormat:@"%@",listItems];
NSLog(@":%@",covert);   //out  10,
                               10,
                               02

float realArrary = [dataString floatValue];
NSLog(@"floatP : %f",realArrary); out 2.030000 10.000000 10.100000

1 个答案:

答案 0 :(得分:0)

尝试

 NSString *dateString = [obj valueForKey:@"oficialTime"];
 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 [dateFormatter setDateFormat:@"hh:mm:ss.SSS"];
 NSDate *dateFromString = [[NSDate alloc] init];
 dateFromString = [dateFormatter dateFromString:dateString];
 NSLog(@"%@",dateString); //Out 02:03:03

然后

NSString * dataString = [TimeRunningString stringByReplacingOccurrencesOfString:@":" withString:@""];             float Realarrary = [dataString floatValue];
NSLog(@"floatP: %0.0f",Realarrary); //Out 20303

 now you have string 02:03:03 to float 20303