How to specify decimal places when formatting NSNumber objects?
Asked Answered
D

3

10

I'm using the following piece of Objective-C code to format a NSNumber, and it works fine most of the time, but it doesn't quite do what I want when the NSNumber object holds an integer (no fractional part).

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(80.0f, 90.0f, 225.0f, 40.0f)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setFormat:@"###.##"];
int bytes = 1024 * 1024;
NSNumber *total = [NSNumber numberWithFloat:([self.totalFileSize floatValue] / bytes)];
label.text = [NSString stringWithFormat:@"0.00 MB of %@ MB", [formatter stringFromNumber:total]];

So for instance, if self.totalFileSize holds 55.2300, it will display "55.23" as it should on my UILabel. But if that same variable holds 55, it will simply display "55" on the label.

What I really want is for this code to always include the 2 decimal places in the output.

How can I achieve that?

Dreddy answered 8/2, 2009 at 0:15 Comment(0)
V
16

Your format string should have a "0" for each decimal place you want to always exist.

[formatter setFormat:@"###.00"];

for "55.23" and ".23" and ".20"

[formatter setFormat:@"##0.00"];

for "55.23" and "0.23" and "0.20"

See Number Format String Syntax (Mac OS X Versions 10.0 to 10.3)

Or you can use a 10.4 formatter with - setMinimumFractionDigits: and - setMaximumFractionDigits: with both set to 2.

Venettavenezia answered 8/2, 2009 at 1:14 Comment(2)
setFormat: Is not available for iOS(might work in simulator, but not on device). You need to use setPositiveFormat: and setNegativeFormat:Glare
on iOS you need to use setMinimumFractionDigits: and/or - setMaximumFractionDigits:Beitz
P
7

I believe what you are looking for is: "%.2f"

Worst case scenario, you can split the value, check if there is anything after the decimal. If there isn't add the 00 by hand?

Pudendas answered 8/2, 2009 at 0:29 Comment(3)
I believe what you meant you believe what he is looking for is: "%.2f" ;)Dirndl
Using that won't do what you think it will, as passing a NSNumber object to NSLog will result in it calling [obj description], which returns a string. In any case, I'm trying to avoid hacks like the "append 00 if it's not there". I'm sure NSNumberFormatter has a feature to do what I want here.Dreddy
@Garret, I was desperate to find this simple solution. Number formatting is not easy. Thanks.Analcite
S
3

You might be better off with localizedStringWithFormat, like so...

[UILabel alloc] initWithFrame:CGRectMake(80.0f, 90.0f, 225.0f, 40.0f)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
int bytes = 1024 * 1024;
label.text = [NSString localizedStringWithFormat:@"0.00 MB of %.2f MB", ([self.totalFileSize floatValue] / bytes)];

And of course, bytes could be a const unsigned, and clearColor tends to be a performance hog, but those are issues for another thread.

Stenson answered 8/2, 2009 at 1:13 Comment(2)
What is the problem with using clearColor and what would be better? Thanks you very much!Analcite
thanks for the localizedStringWithFormat hint. I was desperately looking for a easy way to ensure localized decimal separator.Analcite

© 2022 - 2024 — McMap. All rights reserved.