So, you want to print out a floating point number. Do you want it to as high of accuracy as your computer can muster? In general, you probably don’t. If I was splitting a $43.89 check 4 ways, would you really want to know that the answer is $10.9725? Of course you could round the number yourself, but why, when you can simply use NSNumberFormatter!
Formatting with Style!
So, first things first, what kind of number do you want to work with. Do you want currency like above, or maybe something else? You will need to set your NSNumberFormatter object’s NumberStyle. These are denoted by the aptly (or maybe Apple-tly? I should remember that word) named NSNumberFormatterStyle enum. The available options are….
NSNumberFormatterStyle
- NSNumberFormatterNoStyle
- NSNumberFormatterDecimalStyle
- NSNumberFormatterCurrencyStyle
- NSNumberFormatterPercentStyle
- NSNumberFormatterScientificStyle
- NSNumberFormatterSpellOutStyle
So what do each of these do?
NSNumberFormatterNoStyle
This is by default, an integer formatter, with nothing special. So a value of 24891318.58825 comes out as 24891319. So it rounds appropriately, and then says the integer form of the number.
NSNumberFormatterDecimalStyle
This adds the appropriate separators and decimal points for your locale, and by default is limited to 3 decimal places, so 24891318.58825 comes out as 24,891,318.588 (in en-US locale).
NSNumberFormatterCurrencyStyle
This is similar to NSNumberFormatterDecimalStyle, but with the locale specific currency symbol, so 24891318.58825 comes out as $24,891,318.59 (in en-US locale).
NSNumberFormatterPercentStyle
This one I found a little surprising, but it assumes you already have your percentage in decimal form, so it expects your value of 39.2% to be in the form 0.392. So if we continue with the same number, 24891318.58825 comes out as 2,489,131,859%. Notice the magnitude of this one. The previous answers were all 24 million 891 thousand, etc. This one is 2.489 billion percent. So by default it multiplies the value by 100, and outputs no decimal values.
NSNumberFormatterScientificStyle
This by default puts out the normalized scientific notation of the value. It outputs it in E-Notation, instead of the classic a x 10^b style, but that is common enough in computers. So 24891318.58825 comes out as 2.489131858825E7.
NSNumberFormatterSpellOutStyle
I did not know about this one until I started writing this. This is kind of cool, it actually will write out your value into words. It even will output it in the correct language for your locale! So 24891318.58825 comes out as “twenty-four million eight hundred ninety-one thousand three hundred eighteen point five eight eight two five” (in en-US locale).
How Many digits?
You can also set how many digits. The above styles have some built in settings for this, but they can be overridden. You can set the minimum or maximum number of integer or fractional digits. You just have to modify the appropriate property in your NSNumberFormatter object.
These are:
- minimumIntegerDigits
- maximumIntegerDigits
- minimumFractionDigits
- maximumFractionDigits
Since these are properties, you can either do this through dot syntax, or the appropriate setter in normal message syntax:
//Dot Syntax
yournumberformatter.maximumFractionDigits = 3;
//Message syntax
[yourNumberFormatter setMaximumFractionDigits: 3];
Actually Using NSNumberFormatter
So, how do you actually use this? Lets just use one of the built in formats above for simplicity:
NSNumber *myNumber = @(24891318.58825);
NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
nf.numberStyle = NSNumberFormatterDecimalStyle;
NSLog(@"NSNumberFormatterDecimalStyle - %@", [nf stringFromNumber:myNumber]);
That’s it! All you do is ask the NSNumberFormatter object for a string based off of your NSNumber, and it gives you the appropriate string based off of the settings you give it (in this case, the NSNumberFormatterDecimalStyle).
Conclusion
I have only scratched the surface of what NSNumberFormatter can do. Among other notable capabilities, it can actually read a string and give you an NSNumber back from it, but I’ll leave that to another post.
I hope you found this article helpful. If you did, don’t hesitate to share this post on twitter or your social media of choice. The blog is still pretty new, and every share helps. Of course, if you have any questions, don’t hesitate to contact me on twitter @CodingExplorer, and I’ll see what I can do. Thanks!