Re: Formatting integer value
Re: Formatting integer value
- Subject: Re: Formatting integer value
- From: Sherm Pendley <email@hidden>
- Date: Mon, 14 Feb 2005 08:39:10 -0500
On Feb 14, 2005, at 8:02 AM, René Korthaus wrote:
i have a problem: I read an integer value from an xml file (plist format) which is the duration of a song.
The formatted integer value is like "325407", where 325 are seconds and 407 are milliseconds. Now i want to make a 3:25 string out of it to display the duration of that song in a tableview. I cannot figure out how to drop the 407ms off that integer value.
You can extract substrings using NSString's -substringWithRange: method. This example assumes that seconds are always two digits (zero-padded if necessary), and milliseconds are always three digits. Based on your description of the output you need, it also assumes that 325 means 3 minutes, 25 seconds, not 325 seconds. Minutes can be any number of digits, or missing entirely.
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *time = @"325407";
NSRange theRange = { [time length]-3, 3 };
NSString *milliseconds = [time substringWithRange:theRange];
theRange.location = [time length]-5;
theRange.length = 2;
NSString *seconds = [time substringWithRange:theRange];
theRange.location = 0;
theRange.length = [time length]-5;
NSString *minutes = [time substringWithRange:theRange];
NSLog(@"Time=%@:%@:%@", minutes, seconds, milliseconds);
[pool release];
return 0;
}
sherm--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden