objective c - Prevent small negative numbers printing as "-0" -


if following in objective-c:

nsstring *result = [nsstring stringwithformat:@"%1.1f", -0.01]; 

it give result @"-0.0"

does know how can force result @"0.0" (without "-") in case?

edit: tried using nsnumberformatter, has same issue. following 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]]; 

i wanted general solution, independent of configuration of number formatter.

i've used category add functionality 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 works

the key feature ask number formatter how format -0.0f (i.e., floating point minus zero) nsstring can detect , take remedial action.

why this? depending on formatter configuration, -0.0f formatted as: @"-0", @"-0.0", @"-000", @"-0ยบc", @"£-0.00", @"----0.0", @"(0.0)", @"๐Ÿ˜ก๐Ÿ˜.⓪้›ถ" really, pretty anything. so, ask formatter how format -0.0f using line: nsstring *const negzerostring = [self stringfromnumber: [nsnumber numberwithfloat: -0.0f]];

armed undesired -0.0f string, when arbitrary input number formatted, can tested see if matches undesirable -0.0f string.

the second important feature number formatter asked supply replacement positive 0 string. necessary before, formatting respected. done line: [self stringfromnumber: [nsnumber numberwithfloat: 0.0]]

an optimisation doesn't work

it's tempting perform numerical test whether input number formatted -0.0f string, extremely non trivial (ie, impossible in general). because set of numbers format -0.0f string depend on configuration of formatter. if if happens rounding nearest million, -5,000f input formatted -0.0f string.

an implementation error avoid

when input formats -0.0f string detected, positive 0 equivalent output string generated using [self stringfromnumber: [nsnumber numberwithfloat: 0.0]]. note that, specifically:

  • the code formats float literal 0.0f , returns it.
  • the code does not use negation of input.

negating input of -0.1f result in formatting 0.1f. depending on formatter behaviour, rounded , result in @"1,000", don't want.

final note

for it's worth, approach / pattern / algorithm used here translate other languages , different string formatting apis.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -