---
title: "#134: Formatting Strings with Format 🖊"
url: https://littlebitesofcocoa.com/134-formatting-strings-with-format
author: Jake Marsh
date: 2015-11-26
published: 2015-11-26
topics: ["Libraries","Foundation"]
description: "Formatting strings is one of the most common tasks in building software. It's usually not that complex, but it can be tempting to cut corners."
---

# #134: Formatting Strings with Format 🖊

By Jake Marsh, November 26, 2015. HTML: https://littlebitesofcocoa.com/134-formatting-strings-with-format

Formatting strings is one of the most common tasks in building software. It's usually not that complex, but it can be tempting to cut corners.

When working with things like currency or street addresses, having a robust formatting system in place can be crucial in ensuring our **app** works well all over the world.

Today we'll look at [Format](http://git.io/format), a library from [Roy Marmelstein](https://github.com/marmelroy) that can help us achieve all this with ease.

_Format_ starts by extending Swift's number types to add a format **function**. We can call this on any number (even **literals**) and it will return a **String** with our desired format:

``` swift
134.format(Decimals.Two) // => "134.00"
```

_Format_ makes localizing a breeze. It uses the device's current locale by default, or we can render a specific one:

``` swift
let gb = NSLocale(localeIdentifier: "GB")
87.format(Currency.GBP, locale: gb) // => "£ 87.00"
```

Ordinal numbers can really help class up the joint:

``` swift
134.format(General.Ordinal) // => "134th"
```

_Format_ can format in all sorts of interesting ways, for example:

``` swift
10.11.format(General.SpellOut) // => "ten point one one"
```

Numbers are cool, but what about addresses? Almost every nation has a slightly different convention for how they format street addresses.

_Format_ wraps **CNPostalAddressFormatter** from the **Contacts** **framework** ([Bite #24](https://littlebitesofcocoa.com/24)) to make formatting addresses quite simple:

``` swift
AddressFormatter().format(
  "1 Infinite Loop", 
  city: "Cupertino", state: "CA", postalCode: "95014", 
  country: "USA", ISOCountryCode: "US")

// => "1 Infinite Loop\nCupertino CA 95014\nUSA"
```

_Format_ also extends **CLPlacemark**, adding a **function** to format that **class's** addressDictionary **property**. Users expect things to look familiar. Never underestimate the power of good localization.

More info about _Format_ can be found at [git.io/format](http://git.io/format)
