objective objective-c cocoa-touch string-formatting

objective-c - nsdateformatter objective c



Conversión de int a cadena abreviada y formateada (2)

El usuario ingresará un valor en dólares como int , y me gustaría convertir el resultado en una cadena abreviada y formateada. Entonces, si el usuario ingresa 1700, la cadena diría "$ 1.7k". Si el usuario ingresa 32600000, la cadena dirá "$ 32.6m".

Actualizar

Aquí está el código que tengo hasta ahora. Parece estar funcionando para números ~ 10k. Simplemente agregaría más declaraciones if para números más grandes. Pero, ¿hay una manera más eficiente de hacer esto?

NSNumberFormatter *nformat = [[NSNumberFormatter alloc] init]; [nformat setFormatterBehavior:NSNumberFormatterBehavior10_4]; [nformat setCurrencySymbol:@"$"]; [nformat setNumberStyle:NSNumberFormatterCurrencyStyle]; double doubleValue = 10200; NSString *stringValue = nil; NSArray *abbrevations = [NSArray arrayWithObjects:@"k", @"m", @"b", @"t", nil] ; for (NSString *s in abbrevations) { doubleValue /= 1000.0 ; if ( doubleValue < 1000.0 ) { if ( (long long)doubleValue % (long long) 100 == 0 ) { [nformat setMaximumFractionDigits:0]; } else { [nformat setMaximumFractionDigits:2]; } stringValue = [NSString stringWithFormat: @"%@", [nformat stringFromNumber: [NSNumber numberWithDouble: doubleValue]] ]; NSUInteger stringLen = [stringValue length]; if ( [stringValue hasSuffix:@".00"] ) { // Remove suffix stringValue = [stringValue substringWithRange: NSMakeRange(0, stringLen-3)]; } else if ( [stringValue hasSuffix:@".0"] ) { // Remove suffix stringValue = [stringValue substringWithRange: NSMakeRange(0, stringLen-2)]; } else if ( [stringValue hasSuffix:@"0"] ) { // Remove suffix stringValue = [stringValue substringWithRange: NSMakeRange(0, stringLen-1)]; } // Add the letter suffix at the end of it stringValue = [stringValue stringByAppendingString: s]; //stringValue = [NSString stringWithFormat: @"%@%@", [nformat stringFromNumber: [NSNumber numberWithDouble: doubleValue]] , s] ; break ; } } NSLog(@"Cash = %@", stringValue);


Si quieres una cadena con un ancho ajustable automáticamente, puedes usar:

+(NSString*)numberWithShortcut:(NSNumber*)number { unsigned long long value = [number longLongValue]; NSUInteger index = 0; double dvalue = (double)value; NSArray *suffix = @[ @"", @"K", @"M", @"B", @"T", @"P", @"E" ]; while ((value /= 1000) && ++index) dvalue /= 1000; NSString *svalue = [NSString stringWithFormat:@"%@%@",[NSNumber numberWithDouble:dvalue], [suffix objectAtIndex:index]]; return svalue; }


unsigned long long value = 1700llu; //value = 32600001llu; //value = UINT64_MAX; NSUInteger index = 0; double dvalue = (double)value; //Updated to use correct SI Symbol ( http://en.wikipedia.org/wiki/SI_prefix ) NSArray *suffix = @[ @"", @"k", @"M", @"G", @"T", @"P", @"E" ]; while ((value/=1000) && ++index) dvalue /= 1000; NSString *svalue = [NSString stringWithFormat:@"$%.*f%@", //Use boolean as 0 or 1 for precision (int)(dvalue < 100.0 && ((unsigned)((dvalue - (unsigned)dvalue) * 10) > 0)), dvalue, [suffix objectAtIndex:index]]; NSLog(@"Value: %@", svalue);

Versión localizada ARC

unsigned long long value = 1700llu; //value = 32600001llu; //value = UINT64_MAX; NSUInteger index = 0; double dvalue = (double)value; //Updated to use correct SI Symbol ( http://en.wikipedia.org/wiki/SI_prefix ) NSArray *suffix = @[ @"", @"k", @"M", @"G", @"T", @"P", @"E" ]; while ((value/=1000) && ++index) dvalue /= 1000; NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; //Germany Example [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"de-de"]]; //Set fractional digits to 0 or 1 [formatter setMaximumFractionDigits:(int)(dvalue < 100.0 && ((unsigned)((dvalue - (unsigned)dvalue) * 10) > 0))]; NSString *svalue = [[formatter stringFromNumber:[NSNumber numberWithFloat:dvalue]] stringByAppendingString:[suffix objectAtIndex:index]]; NSLog(@"Value: %@", svalue);