Prevent printing -0

Posted by fishinear on Stack Overflow See other posts from Stack Overflow or by fishinear
Published on 2012-06-10T14:15:16Z Indexed on 2012/06/10 22:40 UTC
Read the original article Hit count: 184

Filed under:
|
|

If I do the following in Objective-C:

NSString *result = [NSString stringWithFormat:@"%1.1f", -0.01];

It will give result @"-0.0"

Does anybody know how I can force a result @"0.0" (without the "-") in this case?

EDIT: I tried using NSNumberFormatter, but it has the same issue. The following also produces @"-0.0":

double value = -0.01;
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setMaximumFractionDigits:1];
[numberFormatter setMinimumFractionDigits:1];
NSString *result = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:value]];

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about nsstring