0

For example, I have the following code, where lblPercent is an NSTextField:

double Progress = progress( Points);
[lblPercent setIntValue:(Progress)];

I set it as integer value so it tosses out the decimal, since for some reason the NSProgressIndicator forces me to use a double. Anyway, in the label adjacent to the progress bar, I want it see the number x% with the percent sign next to it.

I tried standard concatenation techniques but no dice.

NSGod
  • 22,699
  • 3
  • 58
  • 66

6 Answers6

1

You should use an NSNumberFormatter with the percent style

NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle: NSNumberFormatterPercentStyle];
// Any other format settings you want
NSString* formattedNumber = [formatter stringFromNumber: [NSNumber numberWithDouble: progress]];
JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • This gave me a rather bizarre number that wasn't what the variable returned for some reason. – Jonathan Weinraub Apr 23 '13 at 20:50
  • @JonathanWeinraub Using this method to set the field you should use `setStringValue:` on your test field, nt `setIntValue:` – JeremyP Apr 24 '13 at 14:45
  • Sorry for taking so long to get back to you, I had final exams. Anyway, I now understand what you meant. However, I changed it to what you suggested, the value I get is 600% so the percent sign is there. However, my data that is being returned should be 6% not 600%. So far we are making the best progress on this - I think better progress than I did on my exams :-/ – Jonathan Weinraub May 11 '13 at 06:22
  • I assume I do this: [lblPercent setStringValue:(formattedNumber)]; because I just realised it isn't that I got padded with zeroes, I got 750% when I gave it data that should had return 1%. – Jonathan Weinraub May 11 '13 at 06:42
  • @JonathanWeinraub percent style will do the multiplying by 100 for you. formatting 0.06 with percent style should give you 6% because that's what it is. – JeremyP May 15 '13 at 09:34
0

try

[lblPercent setText:[NSString stringWithFormat:@"%d%%",[Progress intValue]]];
alex
  • 2,121
  • 1
  • 18
  • 24
0
NSMutableString *value = lblPercent.text;
[value appendString:@"%"];
[lblPercent setText:value];
Burhanuddin Sunelwala
  • 5,318
  • 3
  • 25
  • 51
  • This gave me text not found in object of type NSTextField * and also no visible @interface for NSTextfield declares the selector setText – Jonathan Weinraub Apr 23 '13 at 15:27
  • @JonathanWeinraub: I've edited your question to make it more clear that you are using Cocoa and `NSTextField`, not `UITextField` in iOS. (This answer assumes `lblPercent` is a `UITextField`, and won't work with `NSTextField`). – NSGod Apr 23 '13 at 16:29
  • @JonathanWeinraub Before your edit, I assumed you were using UILabel as you named it lblPercent. – Burhanuddin Sunelwala Apr 23 '13 at 17:40
  • @nsgod any ideas on how I can accomplish this now we sorted out the question properly? – Jonathan Weinraub Apr 24 '13 at 01:54
0

You can use unicode characters to get the percent sign.

i.e.

double value;

myLabel.text = [NSString stringWithFormat:@"%d\u0025", value ]

u0025 is the unicode character for 'percent sign'

Ríomhaire
  • 3,084
  • 4
  • 25
  • 40
0
NSInteger percentageProgress = (NSInteger) (Progress * 100);
[lblPercent setText:[NSString stringWithFormat:@"%d%%", percentageProgress]];
Mikhail
  • 4,271
  • 3
  • 27
  • 39
0
NSString *string = [NSString stringWithFormat:@"%.0f%@",Progress, @"%"];
[lblPercent setStringValue:string];

This seems to had worked for me doing it the way I had done it...