Question

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];
Was it helpful?

Solution 2

@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]; 

OTHER TIPS

One was it convert the number to a NSString using the number formatter that you have set up. Make sure that the total digits will be more than 10. Then use the string class to truncate the number.

NSString *numbLong = @"123023.123781237912378123"; // get this from your number formatter
NSString *numbTruncated = [numbLong substringToIndex:10]; // 123023.123
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top