I need to format a number with NSNumberFormatter. If the number is small (for example, 0.000001), it will be displayed in decimal format, 0.000001. If the number is large, many decimal or integer (for example, 1211345456 o 0.000000011111553242), will be displayed in scientific notation, 1.211 E9.
I have this code (very bad), but it’s not enough for me: (I think the solution will be in -setPositiveFormat and templates of the formats #, ## 0.0000000 and 0. ### E + 0, any idea? Thanks!
NSNumberFormatter *formato = [[NSNumberFormatter alloc] init]; [formato setNumberStyle:NSNumberFormatterDecimalStyle]; [formato setPositiveFormat:@"#,##0.0000000"]; [formato setNegativeFormat:@"-#,##0.0000000"]; [formato setMaximumFractionDigits:7]; [formato setMinimumFractionDigits:0]; [formato setRoundingMode: NSNumberFormatterRoundHalfDown]; NSString *numero = [formato stringFromNumber:[NSNumber numberWithDouble:valor]];
source share