string formatting

  • hi.
    I have written a method that puts out an NSstring like "12h12m12.34s"
    using a string format. that works ok, but there is something I don't
    like: when the number of minutes or seconds is less than 10, it'll
    put out only 1 digit instead of 2. like this: "1h1m1.23s". is there a
    way to force leading zeros in there, like this: "1h01m01.23s"? i
    don't need a leading zero for the hours.
    Do I have to make 3 NSstrings (for hours, minutes and seconds) with
    leading zeros and then stick them together? I think I can get that
    sorted, but that would be a lot of work. is there an easier way?

    --
    GPG Key ID: 92AD877F7E4E32B9
    Fingerprint: 16B0 7DCD 3AB0 0C45 F5FD  581D 92AD 877F 7E4E 32B9
  • On Oct 3, 2007, at 4:14 PM, H M wrote:

    > hi.
    > I have written a method that puts out an NSstring like
    > "12h12m12.34s" using a string format. that works ok, but there is
    > something I don't like: when the number of minutes or seconds is
    > less than 10, it'll put out only 1 digit instead of 2. like this:
    > "1h1m1.23s". is there a way to force leading zeros in there, like
    > this: "1h01m01.23s"? i don't need a leading zero for the hours.
    > Do I have to make 3 NSstrings (for hours, minutes and seconds) with
    > leading zeros and then stick them together? I think I can get that
    > sorted, but that would be a lot of work. is there an easier way?

    I've been doing something similar, starting with separate numbers,
    formatting, then creating the string. To do this, I use
    NSNumberFormatter.

    http://developer.apple.com/documentation/Cocoa/Reference/Foundation/
    Classes/NSNumberFormatter_Class/Reference/Reference.html

    Maybe a pro on the list can point you in the direction of something
    that you could implement on the string as a whole.

    Jaime Magiera
    Sensory Research
    http://www.sensoryresearch.net
  • Just use standard printf style attributes in your format string:

    someString = [NSString stringWithFormat: @"%dh%02dm%02.2fs", hours,
    minutes, seconds];

    - d

    On Oct 3, 2007, at 4:14 PM, H M wrote:

    > hi.
    > I have written a method that puts out an NSstring like
    > "12h12m12.34s" using a string format. that works ok, but there is
    > something I don't like: when the number of minutes or seconds is
    > less than 10, it'll put out only 1 digit instead of 2. like this:
    > "1h1m1.23s". is there a way to force leading zeros in there, like
    > this: "1h01m01.23s"? i don't need a leading zero for the hours.
    > Do I have to make 3 NSstrings (for hours, minutes and seconds) with
    > leading zeros and then stick them together? I think I can get that
    > sorted, but that would be a lot of work. is there an easier way?
  • It's in the documentation on the string format specifiers: %.2d gives
    you a leading zero when the argument is only one digit.

    Hank

    On Oct 3, 2007, at 4:14 PM, H M wrote:

    > hi.
    > I have written a method that puts out an NSstring like
    > "12h12m12.34s" using a string format. that works ok, but there is
    > something I don't like: when the number of minutes or seconds is
    > less than 10, it'll put out only 1 digit instead of 2. like this:
    > "1h1m1.23s". is there a way to force leading zeros in there, like
    > this: "1h01m01.23s"? i don't need a leading zero for the hours.
    > Do I have to make 3 NSstrings (for hours, minutes and seconds) with
    > leading zeros and then stick them together? I think I can get that
    > sorted, but that would be a lot of work. is there an easier way?
    >
    >
    > --
    > GPG Key ID: 92AD877F7E4E32B9
    > Fingerprint: 16B0 7DCD 3AB0 0C45 F5FD  581D 92AD 877F 7E4E 32B9
    >

    Hank Heijink
    <hank.list...>
  • Stupid floats. I always mess that up.

    Try this:

    someString = [NSString stringWithFormat: @"%dh%02dm%05.2fs", hours,
    minutes, seconds];

    - d

    On Oct 3, 2007, at 4:28 PM, Dave Hersey wrote:

    > Just use standard printf style attributes in your format string:
    >
    > someString = [NSString stringWithFormat: @"%dh%02dm%02.2fs", hours,
    > minutes, seconds];
    >
    > - d
    >
    > On Oct 3, 2007, at 4:14 PM, H M wrote:
    >
    >> hi.
    >> I have written a method that puts out an NSstring like
    >> "12h12m12.34s" using a string format. that works ok, but there is
    >> something I don't like: when the number of minutes or seconds is
    >> less than 10, it'll put out only 1 digit instead of 2. like this:
    >> "1h1m1.23s". is there a way to force leading zeros in there, like
    >> this: "1h01m01.23s"? i don't need a leading zero for the hours.
    >> Do I have to make 3 NSstrings (for hours, minutes and seconds)
    >> with leading zeros and then stick them together? I think I can get
    >> that sorted, but that would be a lot of work. is there an easier way?

  • > someString = [NSString stringWithFormat: @"%dh%02dm%05.2fs", hours,
    > minutes, seconds];

    that did the trick. thanks for the help.
previous month october 2007 next month
MTWTFSS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Go to today