Search code examples
iosobjective-cnsnumberformatter

NSNumberFormatter set multiplier for dividing with large number


I have a NSNumberFormatter and I would like to display currency as in a Billion, So if I have 1,000,000,000 $ I would like to display 1B $

I create NSNumberFormatter object and set multiplyer :

NSNumberFormatter * numberFormatter = [[NSNumberFormatter alloc]init];
[numberFormatter setMultiplier: @(1.0/1000000000)];

If I display multiplier as:

NSLog(@" NUMBER  %@", numberFormatter.multiplier); //  -->  NUMBER  1e-09

But if I put

NSLog(@"Formatter %@", [numberFormatter1 stringFromNumber:@(1225245041496000)]); // --> Formatter 0

It seams to me that multiplier round number but I don't know how to prevent this.


Solution

  • Looks like you're exceeding the minimum value allowed for the multiplier. I can't find docs on it, but quick testing shows:

    [numberFormatter setMultiplier: @(1.0 / 8388608.0)];
    

    works fine, but

    [numberFormatter setMultiplier: @(1.0 / 8388609.0)];
    

    fails (string output is always "0").

    So I don't think NSNumberFormatter is going to fit your needs.