PNCurrency makes it easy to interchange different currency formats within your applicaiton. If you've ever worked with currency in your app before then you know how common of a task it is converting between cents, double, strings or even some formatted variant of those. That's where PNCurrency comes in, it is a simple interface for doing just that. Read on to check out the interface and see how to get started.
PNCurrency is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'PNCurrency', '~> 1.0'
Now you can use it in your project like so
#import <PNCurrency/PNCurrency.h>
You can clone the repo with git:
git clone https://github.com/pinnrepo/PNCurrency.git
then simply add the files to your project, and use a local import header
#import "PNCurrency.h"
Here's some good reasons to use PNCurrency in your next project instead of writing your own:
- 100% documentation coverage
- Thoroughly unit tested
- Cocoapods integration, get started quick
- Simple interface that will drop petty lines from your source
Here's the quick jist of how to get started using PNCurrency
Theres a few different ways to instantiate a new PNCurrency object, here they are to show you how to best init the object for your usage.
The plain init will default the amount to zero. Just alloc init the object like so:
PNCurrency *currency = [[PNCurrency alloc] init];
To create the object when you have an amount in cents use this:
// Initialize currency to a dollar
PNCurrency *currency = [[PNCurrency alloc] initWithCentsAmount:100];
You might have a raw double, in which case you'll want to use this:
// Initialize currency to a dollar
PNCurrency *currency = [[PNCurrency alloc] initWithDoubleAmount:1.00];
Finally if you have a string that represents the amount you can feed that in as well:
// Initialize currency to a dollar
PNCurrency *currency = [[PNCurrency alloc] initWithStringAmount:@"1.00"];
Theres a good chance you're going to want to update the currency value to something else in the objects lifetime, this can be done easily a few different ways
You can modify the amount by changing the centsAmount property:
PNCurrency *currency = [[PNCurrency alloc] init];
currency.centsAmount = 10 // value is now $0.10
Or change it with a new double value:
PNCurrency *currency = [[PNCurrency alloc] init];
[currency setWithDoubleAmount:1.11] // value is now $1.11
Finally with a new string value:
PNCurrency *currency = [[PNCurrency alloc] init];
[currency setWithStringAmount:@"2.00"] // value is now $2.00
Nothing fancy here but safe convenient autoconversions, when you need to actually return the value here's what to do
You can utilize the cents amount property to get the raw amount of cents as an unsigned integer
PNCurrency *currency = [[PNCurrency alloc] initWithStringAmount:@"1.00"];
[currency centsAmounts] // Value is 100 (you can also use currency.centsAmount
Returns the amount as a double value, may be truncated for trailing zeroes
PNCurrency *currency = [[PNCurrency alloc] initWithCentsAmount:100];
[currency doubleAmount] // Value is 1.00
Returns the amount as a string exactly as you would expect
PNCurrency *currency = [[PNCurrency alloc] initWithCentsAmount:100];
NSLog(@"%@", [currency stringAmount]) // prints "1.00"
Often you'll want to print this stuff to the screen so chances are you will want something like "$1.00" as opposed to a double or amount in cents. These are the convenience formatters.
Simply prints the value formatted with the dollar sign right in front
PNCurrency *currency = [[PNCurrency alloc] initWithCentsAmount:100];
NSLog(@"%@", [currency formattedAmount]) // prints "$1.00"
Prints the dollar sign in front of the amount but with a space between the $ and the amount itself
PNCurrency *currency = [[PNCurrency alloc] initWithCentsAmount:100];
NSLog(@"%@", [currency formattedAmountWithSpace]) // prints "$ 1.00"
You can check out the full reference documentation here.
- v1.0 - Initial release (4/1/15)
- v1.1 - Added PNCurrencyTextField (4/4/15)
- v1.2 - Comma formatting on amounts, NSDecimalNumber as internal storage
- v1.3 - Rounding done properly, using bankers rounding by default
Here's a list of features and improvements that are up and coming
- NSCoding and NSCopying compliant
- Rounding mode property
- Setters for just dollars portion or cents portion
- Fork it
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Run
flake8 . --exclude=.venv
- Submit a pull request
- David Westerhoff (david@getpinn.co)
- Tim Co (tim@getpinn.co)
The MIT License (MIT)
Copyright (c) 2016 Pinn Technologies, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.