Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Just to improve the solution, a good idea is maybe to subclass the NSNumberFormatter class and override the - (NSString *)stringForObjectValue:(id)anObject method.</p> <p>Using the code from zardon, I added a statement for the values &lt; 1000 which doesn't format the number.</p> <p>Here is the code of the method : </p> <pre><code>/* Override the stringForObjectValue method from NSNumberFormatter 100 -&gt; 100 1000 -&gt; 1k 1 000 000 -&gt; 1m 1 000 000 000 -&gt; 1b 1 000 000 000 -&gt; 1t */ - (NSString *)stringForObjectValue:(id)anObject { // If we don't get a NSNumber, we can't create the string if (![anObject isKindOfClass:[NSNumber class]]) { return nil; } NSNumberFormatter *nformat = [[NSNumberFormatter alloc] init]; // Decimal value from the NSObject double doubleValue = [anObject doubleValue]; NSString *stringValue = nil; // Abbrevations used NSArray *abbrevations = [NSArray arrayWithObjects:@"k", @"m", @"b", @"t", nil] ; // If the value is less than 1000, we display directly the value if(doubleValue &lt; 1000.0) { stringValue = [NSString stringWithFormat: @"%@", [nformat stringFromNumber: [NSNumber numberWithDouble: doubleValue]] ]; } else { // Otherwise we format it as expected for (NSString *s in abbrevations) { doubleValue /= 1000.0 ; if ( doubleValue &lt; 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]; break; } } } [nformat release]; return stringValue; } </code></pre> <p>In the interface we simply add the inheritage statement :</p> <pre><code>@interface MoneyNumberFormatter : NSNumberFormatter </code></pre> <p>Hope this helps..</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload