i’m looking for a way to represent a double value in a NSString meeting the following format requirements:
1.) no trailing zeros: x = 0.5000000 -> @”0.5″
2.) at least on decimal: x = 45 -> @”45.0″
3.) maximum 8 decimal characters (last one rounded): x = 0.000000005 -> @”0.00000001″
I tried with @”%.8f”, @”%g” or @”%.8g” but all fail for at least one of the requirements.
I think I could do with @”%.8f” and then loop the characters of the string beginning with the last character of string to the front deleting the “0” characters,
Or start with a “%.8g” and append a “.0” if the string does not contain a decimal point.
Is there any smarter solution available?
You can do this with a number formatter, but you need to create an NSNumber with your double first: