0

I am displaying a string to the user while uploading a video which says something like

20% uploaded

So when the percentage number is single-digit I would like the space for ten's place to be filled by a white space character.

Like How we show 01,02,03 for single - digits as a solution many times, I would like to show white space instead of that 0.

autistic
  • 1
  • 3
  • 35
  • 80
Amogh Talpallikar
  • 12,084
  • 13
  • 79
  • 135

4 Answers4

3

modifiable lvalue's answer is proper to get preferred space between % and digit . YOu just have to check the value , and accordingly locate the "%" sign. Or There is another way .

e.g. I am displaying % sign using following format.

[NSString stringWithFormat:@"%.0f%% of 100% uploaded..",progressValue*10];

This accordingly manages space between your value and '%' sign.

iCoder
  • 1,298
  • 1
  • 9
  • 25
  • I didn't get the alignment by trying this method. Also, `100%` should be `100%%`. – tolgamorf Apr 16 '13 at 06:40
  • Yes It was my mistake , there should be 100%% . But I am able to manage the space between digit n % sign using '.0f' format. – iCoder Apr 16 '13 at 06:44
2

You need to do by setting the format to %<alighmentInteger>d

You can store the value into a stringWithFormat:

Only for showing I am directly NSLogging.

    NSLog(@"%3d%%",i); //this will pad 3 white spaces. 

If you want 2 white spaces use 2d

E.g.:

for (int i=0; i<101; i+=25) {
    NSLog(@"%3d%% uploaded",i);
}

Output:

2013-04-16 11:55:16.002 DynamicObject[48056:303]   0% uploaded
2013-04-16 11:55:16.003 DynamicObject[48056:303]  25% uploaded
2013-04-16 11:55:16.003 DynamicObject[48056:303]  50% uploaded
2013-04-16 11:55:16.004 DynamicObject[48056:303]  75% uploaded
2013-04-16 11:55:16.004 DynamicObject[48056:303] 100% uploaded
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • Much effective solution than my answer; didn't know that, thanks. – tolgamorf Apr 16 '13 at 06:28
  • @tolgamorf: even your answer is same as mine. I posted this in comment 11 minutes ago, and waited no one gave correct answer, so i finally posted it here. – Anoop Vaidya Apr 16 '13 at 06:30
  • By that time I was working on it, so didn't see your comment. :) Now that I think of it, in my answer, the alignment would be lost when the progress is `100%`. – tolgamorf Apr 16 '13 at 06:32
  • Your answer also works but I can mark only one as correct. You specify 3 or 2 in the format he did it as argument. I slightly prefer the other approach as number of digits is a parameter which can be controlled better :) – Amogh Talpallikar Apr 16 '13 at 06:54
1

The best idea I can think of is to check the value. If it's less than 10, print a whitespace character. You could use %.*s to incorporate this into printf: printf("%.*s%d%% uploaded", value < 10, " ", value);

EDIT: It appears, by the specification, that the field width * (or a decimal integer) does this for you: printf("%*d%% uploaded", 2, value); (or printf("%02d%% uploaded", 2, value);)

Amogh Talpallikar
  • 12,084
  • 13
  • 79
  • 135
autistic
  • 1
  • 3
  • 35
  • 80
  • What when 100%, alignment will be lost. – Anoop Vaidya Apr 16 '13 at 06:35
  • in my case, I dont show 100% at all, once the upload is done I show the time since video was uploaded which is "Just Now" instead of 100%. So only while the upload is going on I need to show percentage which wont exceed 2 digits. and even if it does I can keep space for 3 digits instead of 2. Like this: printf("%*d%% uploaded", 3, value) Alignment will always be there. – Amogh Talpallikar Apr 16 '13 at 06:39
1

Here is a method which returns the desired output

- (NSString *)uploadProgress:(NSUInteger)progress {
    return [NSString stringWithFormat:@"%@%% uploaded", progress < 10 ?
            [NSString stringWithFormat:@" %i", progress] :
            [NSString stringWithFormat:@"%i", progress]];
}

Call:

NSLog(@"%@", [self uploadProgress:5]);
NSLog(@"%@", [self uploadProgress:50]);

Output:

2013-04-16 02:24:49.948 stackoverflow test001[11587:c07]  5% uploaded
2013-04-16 02:24:49.949 stackoverflow test001[11587:c07] 50% uploaded
tolgamorf
  • 809
  • 6
  • 23