Search code examples
ioscalculatornsnumberformatter

NSNumberFormatter IOS Calculator display


Please somebody, help me. I am making Calculator App, and I used NSNumberFormatter . I want to set the limit of numbers showing at my UILabel, for example, maximum 12 digits, but it means , any digits even if "1234121.38685 (12)". So if I multiply this number, it is being increasing and it must be rounded correctly, and I want it to be for example, "9785123124.12", if again, then "75647689751.5". So that the result-value showed maximum of integer part, not of float. How to make it ?

numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setGroupingSeparator:@" "];
[numberFormatter setGroupingSize:3];
[numberFormatter setUsesGroupingSeparator:YES];
[numberFormatter setDecimalSeparator:@"."];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setMaximumFractionDigits:10];
[numberFormatter setRoundingMode:NSNumberFormatterRoundUp];
[numberFormatter setMaximumIntegerDigits:10];

Solution

  • @Adam Behringer , it is much more better to use this code for getting RoundMode correct !!

    result=[[self math] calculations]; 
    resultString=[NSString stringWithFormat:@"%.16f", result];
     getIntPartOfValue=result; 
    stringForGetIntPartOfValue=[NSString stringWithFormat:@"%lld",getIntPartOfValue];
    [numberFormatter setMaximumFractionDigits:(maximumAmountOfDigits stringForGetIntPartOfValue.length)+1];