1

I am making a game that requires me to use very large numbers. I believe I am able to store very large numbers with NSDecimal. However, when displaying the numbers to users I would like to be able to convert the large number to a succinct string that uses characters to signify the value eg. 100,000 -> 100k 1,000,000 -> 1.00M 4,200,000,000 -> 4.20B and so forth going up to extremely large numbers. Is there any built in method for doing so or would I have to use a bunch of

NSDecimalCompare statements to determine the size of the number and convert?

I am hoping to use objective c for the application.

I know that I can use NSString *string = NSDecimalString(&NSDecimal, _usLocale); to convert to a string could I then do some type of comparison on this string to get the result I'm looking for?

Richard D
  • 142
  • 8

2 Answers2

-1

Use this method to convert your number into a smaller format just as you need:

-(NSString*) suffixNumber:(NSNumber*)number
{
    if (!number)
        return @"";

    long long num = [number longLongValue];

    int s = ( (num < 0) ? -1 : (num > 0) ? 1 : 0 );
    NSString* sign = (s == -1 ? @"-" : @"" );

    num = llabs(num);

    if (num < 1000)
        return [NSString stringWithFormat:@"%@%lld",sign,num];

    int exp = (int) (log(num) / 3.f); //log(1000));

    NSArray* units = @[@"K",@"M",@"G",@"T",@"P",@"E"];

    return [NSString stringWithFormat:@"%@%.1f%@",sign, (num / pow(1000, exp)), [units objectAtIndex:(exp-1)]];
}

Some sample examples:

NSLog(@"%@",[self suffixNumber:@99999]); // 100.0K
NSLog(@"%@",[self suffixNumber:@5109999]); // 5.1M

Source

Community
  • 1
  • 1
atulkhatri
  • 10,896
  • 3
  • 53
  • 89
  • 1
    unfortunately I need to use numbers larger then what can be stored in NSNumber i need to use NSDecimal. – Richard D Mar 20 '16 at 19:39
-1

Solved my issue: Can only be used if you know that your NSDecimal that you are trying to format will only be a whole number without decimals so make sure you round when doing any math on the NSDecimals.

-(NSString *)returnFormattedString:(NSDecimal)nsDecimalToFormat{

NSMutableArray *formatArray = [NSMutableArray arrayWithObjects:@"%.2f",@"%.1f",@"%.0f",nil];

NSMutableArray *suffixes = [NSMutableArray arrayWithObjects:@"k",@"M",@"B",@"T",@"Qa",@"Qi",@"Sx",@"Sp",@"Oc",@"No",@"De",@"Ud",@"Dud",@"Tde",@"Qde",@"Qid",@"Sxd",@"Spd",@"Ocd",@"Nvd",@"Vi",@"Uvi",@"Dvi",@"Tvi", nil];

int dick = [suffixes count];

NSLog(@"count %i",dick);

NSString *string = NSDecimalString(&nsDecimalToFormat, _usLocale);
NSString *formatedString;
NSUInteger characterCount = [string length];
if (characterCount > 3) {

    NSString *trimmedString=[string substringToIndex:3];

    float a;

    a = 100.00/(pow(10, (characterCount - 4)%3));
    int remainder = (characterCount-4)%3;


    int suffixIndex = (characterCount + 3 - 1)/3 - 2;
    NSLog(@"%i",suffixIndex);
    if(suffixIndex < [suffixes count]){
        NSString *formatSpecifier = [formatArray[remainder] stringByAppendingString:suffixes[suffixIndex]];
        formatedString= [NSString stringWithFormat:formatSpecifier,  [trimmedString floatValue] / a];

    }
    else {
        formatedString = @"too Big";
    }

}
else{
    formatedString = string;
}

return formatedString;

}
Richard D
  • 142
  • 8
  • You can probably compact that into one simple formula that will convert `characterCount` to index into `suffixes`. Something like `(characterCount - 1) / 3 - 1` – Sulthan Mar 22 '16 at 16:10
  • Compute the index for `suffixes` using `index = ceil((characterCount - 6) / 3.0f)` – Joost Mar 22 '16 at 16:10
  • thanks guys just realized that after i posted it and realized i could just round up the character count/3 and subtract two to get the index – Richard D Mar 22 '16 at 16:13
  • Beware of index out of bounds! Also, `(characterCount + 3 - 1)/3 - 2` is incorrect. – Joost Mar 22 '16 at 16:19
  • Ah yes it is in fact correct. The `-1` output for too short strings tripped me up. – Joost Mar 22 '16 at 16:43