Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-09-2010, 07:42 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 67
iloveisrael is on a distinguished road
Default Simple Stopwatch Question

Hey guys,
I've been trying to create a stopwatch, and it's all going good except for the way I format my string.

I want the timer to display the time like this: "00:00:00" (hrs:mins:secs).
I tried this: "[NSString stringWithFormat:@"%d:%d:%d", hrs, mins, secs];", but it displays it at "0:0:0".

Does anyone know how to display the time as I want it?

Thanks so much!!
__________________
Check My Speed
iloveisrael is offline   Reply With Quote
Old 08-09-2010, 07:53 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by iloveisrael View Post
Hey guys,
I've been trying to create a stopwatch, and it's all going good except for the way I format my string.

I want the timer to display the time like this: "00:00:00" (hrs:mins:secs).
I tried this: "[NSString stringWithFormat:@"%d:%d:%d", hrs, mins, secs];", but it displays it at "0:0:0".

Does anyone know how to display the time as I want it?

Thanks so much!!
You want
Code:
[NSString stringWithFormat:@"%2d:%2d:%2d", hrs, mins, secs];
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 08-09-2010, 08:11 PM   #3 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,549
RLScott is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
You want
Code:
[NSString stringWithFormat:@"%2d:%2d:%2d", hrs, mins, secs];
But it would be even better with @"%02d:%02d:%02d" as the format.
RLScott is offline   Reply With Quote
Old 08-09-2010, 08:29 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by RLScott View Post
But it would be even better with @"%02d:%02d:%02d" as the format.
Oops. That's what I meant to type. Thanks for catching that.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 08-09-2010, 09:51 PM   #5 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 67
iloveisrael is on a distinguished road
Default

You guys are awesome!!

Thank you!!!!!!
__________________
Check My Speed
iloveisrael is offline   Reply With Quote
Old 08-09-2010, 10:47 PM   #6 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 27
abhimanipal is on a distinguished road
Default

Quick question what is the difference between the 3 formats ?Could you give me a small exampe ?
abhimanipal is offline   Reply With Quote
Old 08-09-2010, 11:15 PM   #7 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 67
iloveisrael is on a distinguished road
Default

Quote:
Originally Posted by abhimanipal View Post
Quick question what is the difference between the 3 formats ?Could you give me a small exampe ?
All I know is that:
@"%d:%d:%d" = "0:0:0"
@"%2d:%2d:%2d" = " 0: 0: 0"
@"%02d:%02d:%02d" = "00:00:00"

The only question I have is: why?
What's the syntax for these types of things? I've seen also people writing @"%0.0f" and stuff like that, but I could never distinguish between the differences...

Thanks!
__________________
Check My Speed
iloveisrael is offline   Reply With Quote
Old 08-10-2010, 05:13 AM   #8 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,549
RLScott is on a distinguished road
Default

Quote:
Originally Posted by iloveisrael View Post
All I know is that:
@"%d:%d:%d" = "0:0:0"
@"%2d:%2d:%2d" = " 0: 0: 0"
@"%02d:%02d:%02d" = "00:00:00"

The only question I have is: why?
What's the syntax for these types of things? I've seen also people writing @"%0.0f" and stuff like that, but I could never distinguish between the differences...

Thanks!
Google "printf format specifiers"
RLScott is offline   Reply With Quote
Old 08-10-2010, 08:02 AM   #9 (permalink)
Registered Member
 
Ice_2k's Avatar
 
Join Date: Apr 2010
Location: Bucharest, Romania
Posts: 148
Ice_2k is on a distinguished road
Default

%02d = at least 2 digits, if less than two then add 0 at the beginning
Ice_2k is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 325
8 members and 317 guests
ilmman, iram91419, linkmx, nadav@webtview.com, Objective Zero, Paul Slocum, stanny, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,656
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, iram91419
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 11:41 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0