ios - NSString stringWithFormat cant set precision for exponential record -
i'm trying display small double value on textfield
. example, docalculationforequalpressed
function returns 0.00008, when display on text field shows exponential record (8e-05). don't need number shown in exponential view. how set precision when use exponential record ???
using specifier %.9g - doesn't help.
double result; result = [self.brain docalculationforequalpressed:[self.brain operationarray]]; if (result == infinity || result == -infinity || isnan(result)){ nsstring *infinity = @"\u221e"; self.displayfield.text = [nsstring stringwithformat:@"%@", infinity]; } else self.displayfield.text = [nsstring stringwithformat:@"%.9g", result];
it can't done format specifier default.
you need use sprintf , remove trailing zeros yourself.
char str[50]; sprintf (str,"%.20g",num); // make number. morphnumericstring (str, 3); void morphnumericstring (char *s, int n) { char *p; int count; p = strchr (s,'.'); // find decimal point, if any. if (p != null) { count = n; // adjust more or less decimals. while (count >= 0) { // maximum decimals allowed. count--; if (*p != '\0') // if there's less desired. break; p++; // next character. } *p-- = '\0'; // truncate string. while (*p == '0') // remove trailing zeros. *p-- = '\0'; if (*p == '.') { // if decimals zeros, remove ".". *p = '\0'; } } }
see answer
Comments
Post a Comment