Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pglongo/pglformatter
Simple container of NSFormatter and useful functions to format numbers and dates
https://github.com/pglongo/pglformatter
bytecountformatter datecomponentsformatter dateformatter formatter lengthformatter massformatter numberformatter swift
Last synced: about 1 month ago
JSON representation
Simple container of NSFormatter and useful functions to format numbers and dates
- Host: GitHub
- URL: https://github.com/pglongo/pglformatter
- Owner: PGLongo
- License: mit
- Created: 2015-03-16T16:48:38.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-11-24T13:18:44.000Z (almost 7 years ago)
- Last Synced: 2024-09-16T00:23:41.244Z (about 2 months ago)
- Topics: bytecountformatter, datecomponentsformatter, dateformatter, formatter, lengthformatter, massformatter, numberformatter, swift
- Language: Swift
- Homepage:
- Size: 64.5 KB
- Stars: 7
- Watchers: 1
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PGLFormatter
PGLFormatter is a simple container of NSFormatter and useful functions
[![Build Status](https://travis-ci.org/PGLongo/PGLFormatter.svg?branch=master)](https://travis-ci.org/PGLongo/PGLFormatter)
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
[![CocoaPods](https://img.shields.io/cocoapods/v/PGLFormatter.svg)](https://cocoapods.org/pods/PGLFormatter)##Reason
Formatters are extremely expensive to create so it's very important to reuse the formatter as much as you can.This project is inspired from this [article](http://nshipster.com/nsformatter/) published on [NSHipster](http://nshipster.com/)
If you use the same formatter in the entire project you can set in a single point (e.g. AppDelegate) and then forget about the NSFormatter and simple use the function that PGLFormatter offers, for format() to format Double Float Int (see the example), otherwise you should remember to set the behavior every time
## Add PGLFormatter to your project#### Cocoapods
To integrate PGLFormatter into your Xcode project using [Cocoapods](http://cocoapods.org/), specify it in your Podfile:
use_frameworks!
pod 'PGLFormatter'#### Carthage
To integrate PGLFormatter into your Xcode project using [Carthage](https://github.com/Carthage/Carthage), specify it in your Cartfile:
github "PGLongo/PGLFormatter"
## Example### Double Float Int
```swift
let price:Double = 12356789.4200var formatter = PGLFormatter.numberFormatter
// set the formatter behavior
formatter.numberStyle = .CurrencyStyleprice.format() // $12,356,789.42
```#### Formatting Byte
```swift
var formatter = PGLFormatter.byteFormatter
// set the formatter behavior42.formatToByte() // "42 bytes"
42000.formatToByte() // "42 KB"
Int64(42000000000).formatToByte() // 42 GB
```#### Formatting Lenght
```swift
var formatter = PGLFormatter.lenghtFormatter
// set the formatter behavior42.formatLenght(.Meter) // 42 m
42.formatLenghtFromMeters() // 45.931 yd
```#### Formatting Mass
```swift
var formatter = PGLFormatter.massFormatter
// set the formatter behavior42.formatMass(.Gram) // 42 g
42.formatMassFromKilograms() // 92.594 lb
```### NSDate
```swift
let calendar = NSCalendar.currentCalendar()
let components = NSDateComponents()
components.year = 1988
components.month = 11
components.day = 30
components.hour = 0
components.minute = 0
components.second = 0
let date = calendar.dateFromComponents(components)date.format() // 11/30/88, 12:00 AM
```
### TimeFormatter
```swift
PGLFormatter.timeFormatter
formatter.unitsStyle = .Abbreviated
// set the formatter behavior42.formatTime()! // 42s
4200.formatTime()! // 1h 10m```
## Documentation
Documentation is generated and manteined by Cocoapods. You can read the latest documentation at the following [URL](http://cocoadocs.org/docsets/PGLFormatter) and add the docs to [Dash](http://kapeli.com/dash).- [0.0.1](http://cocoadocs.org/docsets/PGLFormatter/0.0.1/)
- [0.0.2](http://cocoadocs.org/docsets/PGLFormatter/0.0.2/)
- [0.0.3](http://cocoadocs.org/docsets/PGLFormatter/0.0.3/)
- [0.0.3](http://cocoadocs.org/docsets/PGLFormatter/0.0.3/)
- [1.0](http://cocoadocs.org/docsets/PGLFormatter/1.0/)
- [1.1](http://cocoadocs.org/docsets/PGLFormatter/1.1/)
- [2.0](http://cocoadocs.org/docsets/PGLFormatter/2.0/)
- [2.1](http://cocoadocs.org/docsets/PGLFormatter/2.1/)
- [3.0](http://cocoadocs.org/docsets/PGLFormatter/3.0/)
- [3.1.0](http://cocoadocs.org/docsets/PGLFormatter/3.1.0/)## Task
- [x] Support Cocoapods
- [x] Support Carthage
- [x] Add installation instruction
- [x] Add other formatters
- [x] Add documentation
- [ ] Implements other useful functions## License
PGLFormatter is released under the MIT license. See LICENSE for details.