Reputation: 109
{
duration = "00:06:29";
id = 7;
}
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
float value = [numberFormatter numberFromString:currentAutio.duration].floatValue;
NSLog(@"Current audio %f",value);
out is Current audio 0.0000
How can I get the duration string to float value?
Upvotes: 2
Views: 94
Reputation: 39988
The easiest way is
NSString *duration = @"00:06:29";
NSArray *array = [duration componentsSeparatedByString:@":"];
NSLog(@"Current hrs %ld",(long)[(NSString *)(array[0]) integerValue]);
NSLog(@"Current min %ld",(long)[(NSString *)(array[1]) integerValue]);
NSLog(@"Current sec %ld",(long)[(NSString *)(array[2]) integerValue]);
Otherwise you can use NSDate
& NSDateFormatter
as well.
Using NSDate
, note that the hours section should not exceed the 23 otherwise result may not be as expected.
NSString *duration = @"00:06:29";
NSDateFormatter *formatter = [NSDateFormatter new];
formatter.dateFormat = @"HH:mm:ss";
NSDate *date = [formatter dateFromString:duration];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:date];
NSLog(@"hrs %ld", (long)components.hour);
NSLog(@"min %ld", (long)components.minute);
NSLog(@"sec %ld", (long)components.second);
Upvotes: 2