KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
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?
Tags (comma-separated)
Save Edits
Cancel