Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format a float to display in a string with two digits?

NSString *timerend = [[NSString alloc] initWithFormat:@"%.0f:%.0f:%02d", hours, minutes, seconds];

So I have this string and a bunch of floats. For example, say seconds = 6.54. It will display as that. I want it to display as 06.54. Is there anyway to do that? Any help with be greatly appreciated.

like image 327
kernelpanic Avatar asked Feb 09 '11 02:02

kernelpanic


1 Answers

Assuming you want the format 'XX:XX:XX.XX' you can use the following code:

    NSString *timerend = [[NSString alloc] initWithFormat:@"%02.0f:%02.0f:%05.2f", hours, minutes, seconds];

When formatting float the number before the point (05) determines the minimum total characters in the entire string, not just the bit before the point.

like image 123
Luke Avatar answered Oct 18 '22 08:10

Luke