iOS convert large numbers to smaller format
Here are two methods I have come up with that work together to produce the desired effect. This will also automatically round up. This will also specify how many numbers total will be visible by passing the int dec.
Also, in the float to string method, you can change the @"%.1f"
to @"%.2f"
, @"%.3f"
, etc to tell it how many visible decimals to show after the decimal point.
For Example:
52935 ---> 53K
52724 ---> 53.7K
-(NSString *)abbreviateNumber:(int)num withDecimal:(int)dec {
NSString *abbrevNum;
float number = (float)num;
NSArray *abbrev = @[@"K", @"M", @"B"];
for (int i = abbrev.count - 1; i >= 0; i--) {
// Convert array index to "1000", "1000000", etc
int size = pow(10,(i+1)*3);
if(size <= number) {
// Here, we multiply by decPlaces, round, and then divide by decPlaces.
// This gives us nice rounding to a particular decimal place.
number = round(number*dec/size)/dec;
NSString *numberString = [self floatToString:number];
// Add the letter for the abbreviation
abbrevNum = [NSString stringWithFormat:@"%@%@", numberString, [abbrev objectAtIndex:i]];
NSLog(@"%@", abbrevNum);
}
}
return abbrevNum;
}
- (NSString *) floatToString:(float) val {
NSString *ret = [NSString stringWithFormat:@"%.1f", val];
unichar c = [ret characterAtIndex:[ret length] - 1];
while (c == 48 || c == 46) { // 0 or .
ret = [ret substringToIndex:[ret length] - 1];
c = [ret characterAtIndex:[ret length] - 1];
}
return ret;
}
Hope this helps anyone else out who needs it!
Swift: Formatting currencies with localized million and billion text
You will have to implement your own solution, but it is not hard. One way to go would be to create two dictionaries where the key will be the Locale identifier and the value the translation:
let millionTrans = [ "en_US" : "million", ...]
let billionTrans = [ "en_US': "billion", ...]
then get the Locale.current
, find out if the amount is in the millions or billions and query the appropriate dictionary to get its value.
Swift. Storing big array of numbers and calculating average value
10,000 doubles, at 8 bytes per item, is only 80,000 bytes (80k). That's a pretty small amount of data. You can use an in-memory array of Doubles for that.
Performance-wise, it would take a modern iPhone a fraction of a second to do 10,000 additions and a division. You can calculate the average any time you want without much speed penalty. It's only if you're doing it over and over in a loop where you'd see a performance penalty.
As pvg pointed out in his comment, if all you ever need is the average then you can simply store a sum and the number of values, which would only be 2 doubles, not 10,000 of them. (You'd just add each new value to the sum, increment the count, and then recalculate the average as:
ave = sum/count
You could also cast your array to an NSArray and write it to a plist quite easily.
Related Topics
Swift Generics and Protocols Not Working on Uikit [Possible Bug]
C++ Dylib in Swift Project - Undefined Symbols for Function Exposed in Dylib
Swift: Filter a Dictionary with Array as Value
Getting the Time Remaining in the Time Interval of a Timer in Swift
Show Datepicker Hourandminute on Print Statement Swiftui
Getting the Count of an Optional Array as a String, or Nil
How to Simultaneously Satisfy Constraints Error Whats Causing This
What Does the Underscore in a Function Declaration Do
Ternary Operator Inside Navigationlink Swiftui
Realitykit - How to Add Motion to a Loaded Modelentity from Usdz File
Learning Swift: Expressions Are Not Allowed at the Top Level
How to Create a Dynamic Link in Firebase Programmatically? Swift