How can I get a simple comma every 3 digits in my result strings?
The code is as follows:
float convertFrom = [[_convertRates objectAtIndex:[picker selectedRowInComponent:0]] floatValue];
float convertTo = [[_convertRates objectAtIndex:[picker selectedRowInComponent:1]] floatValue];
float input = [inputText.text floatValue];
float to = convertTo;
float from = convertFrom;
float convertValue = input;
float relative = to / from;
float result = relative * convertValue;
NSString *convertFromName = [_convertFrom objectAtIndex:[picker selectedRowInComponent:0]];
NSString *convertToName = [_convertFrom objectAtIndex:[picker selectedRowInComponent:1]];
NSString *resultString = [[NSString alloc]initWithFormat:
@" %.4f %@",result, convertToName];
resultLabel.text = resultString;
NSString *formelString = [[NSString alloc]initWithFormat:
@" %.4f %@=", convertValue, convertFromName];
formelLabel.text = formelString;
This code makes a lot of digits, displayed in a block of text, which is not the most practical way to use the data. How can I implement commas in this code?
For example, 1234567 would be 1 234 567 or 1'234'567.
Use NSNumberFormatter.
where
someNumberValueis an NSNumber object with the number.This will format the number properly for the user’s chosen region formatting.
You can control the number of digits after the decimal if you wish. See the docs for
NSNumberFormatterfor more details.