Prevent printing small negative numbers like "-0"

If I do the following in Objective-C:

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

It will give the result @"-0.0"

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

EDIT: I tried using NSNumberFormatter, but it has the same problem. The following also creates @"-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]]; 
+4
source share
4 answers

I wanted a general solution, regardless of the number formatting configuration.

I used the category to add functions to NSNumberFormater;

 @interface NSNumberFormatter (PreventNegativeZero) - (NSString *)stringFromNumberWithoutNegativeZero:(NSNumber *)number; @end 

With implementation:

 @implementation NSNumberFormatter (PreventNegativeZero) - (NSString *)stringFromNumberWithoutNegativeZero:(NSNumber *)number { NSString *const string = [self stringFromNumber: number]; NSString *const negZeroString = [self stringFromNumber: [NSNumber numberWithFloat: -0.0f]]; if([string isEqualToString: negZeroString]) { NSString *const posZeroString = [self stringFromNumber: [NSNumber numberWithFloat: 0.0]]; return posZeroString; } return string; } @end 

How it works

Function is to set the formatting number, how it will format -0.0f (i.e. floating point minus zero) as NSString so that we can detect this and take corrective measures.

What for? Depending on the formatting configuration, -0.0f can be formatted as: @"-0" , @"-0.0" , @"-000" , @"-0ΒΊC" , @"Β£-0.00" , @"----0.0" , @"(0.0)" , @"😑𝟘.β“ͺι›Ά" really, almost everything. So, we’ll ask you to format how it will format -0.0f using the line: NSString *const negZeroString = [self stringFromNumber: [NSNumber numberWithFloat: -0.0f]];

Armed with the -0.0f unwanted line, when an arbitrary input number is formatted, you can test it to see if it -0.0f unwanted line.

The second important feature is that the number of formatter is also proposed to put a replacement positive zero string. This is necessary so that its formatting is still respected. This is done using the string: [self stringFromNumber: [NSNumber numberWithFloat: 0.0]]

Optimization that doesn't work

It is mysterious to perform a numerical test on whether the input number will be formatted as a string -0.0f , but this is extremely nontrivial (i.e., generally impossible at all). This is because the set of numbers that will be formatted in the -0.0f line depends on the formatting configuration. If, if rounding is rounded to the nearest million, then -5,000f as input will be formatted as the string -0.0f .

Implementation error to avoid

Entering these formats into the -0.0f line reveals a positive zero equivalent output line using [self stringFromNumber: [NSNumber numberWithFloat: 0.0]] . Please note that in particular:

  • The code formats the floating literal 0.0f and returns it.
  • The code does not use the negation of input.

-0.1f input -0.1f will result in 0.1f formatting. Depending on the formatting behavior, this can be rounded and lead to @"1,000" , which you don't need.

Final note

For what it's worth, the approach / template / algorithm used here will translate into other languages ​​and various string formatting APIs.

+5
source

Use NSNumberFormatter . In general, NSString formatting should not be used to present data to the user.

EDIT: As stated in the question, this is not the correct answer. There are a number of solutions. It is easy to check a negative zero because it is defined as equal to any zero ( 0.0f == -0.0f ), but the actual problem is that a number of other values ​​can be rounded to negative zero. Instead of catching such values, I suggest post-processing - a function that checks if the result contains only zero digits (omitting other characters). If yes, remove the minus sign.

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

If you pass an instance instead of a value, you can check:

 float myFloat = -0.01; NSString *result = [NSString stringWithFormat:@"%1.1f", (myFloat<0? myFloat*-1:myFloat)]; 

Edit: If you just want 0.0 as a positive value:

  NSString *result = [NSString stringWithFormat:@"%1.1f",(int)(myFloat*10)<0?myFloat:myFloat*-1]; 
0
source

Convert the number to NSString, taking the value float or double. Convert the string to NSNumber.

 NSDecimalNumber *num = [NSDecimalNumber decimalNumberWithString:@"-0.00000000008"]; NSString *st2 = [NSString stringWithFormat:@"%0.2f", [num floatValue]]; NSDecimalNumber *result = [NSDecimalNumber decimalNumberWithString:st2]; //returns 0 
0
source

All Articles