PGLFormatter
Simple container of NSFormatter and useful functions to format numbers and dates
Install / Use
/learn @PGLongo/PGLFormatterREADME
PGLFormatter
PGLFormatter is a simple container of NSFormatter and useful functions
##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 published on NSHipster
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, specify it in your Podfile:
use_frameworks!
pod 'PGLFormatter'
Carthage
To integrate PGLFormatter into your Xcode project using Carthage, specify it in your Cartfile:
github "PGLongo/PGLFormatter"
Example
Double Float Int
let price:Double = 12356789.4200
var formatter = PGLFormatter.numberFormatter
// set the formatter behavior
formatter.numberStyle = .CurrencyStyle
price.format() // $12,356,789.42
Formatting Byte
var formatter = PGLFormatter.byteFormatter
// set the formatter behavior
42.formatToByte() // "42 bytes"
42000.formatToByte() // "42 KB"
Int64(42000000000).formatToByte() // 42 GB
Formatting Lenght
var formatter = PGLFormatter.lenghtFormatter
// set the formatter behavior
42.formatLenght(.Meter) // 42 m
42.formatLenghtFromMeters() // 45.931 yd
Formatting Mass
var formatter = PGLFormatter.massFormatter
// set the formatter behavior
42.formatMass(.Gram) // 42 g
42.formatMassFromKilograms() // 92.594 lb
NSDate
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
PGLFormatter.timeFormatter
formatter.unitsStyle = .Abbreviated
// set the formatter behavior
42.formatTime()! // 42s
4200.formatTime()! // 1h 10m
Documentation
Documentation is generated and manteined by Cocoapods. You can read the latest documentation at the following URL and add the docs to Dash.
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.
Related Skills
node-connect
346.8kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
107.6kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
346.8kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
346.8kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
