Improve numbers readability with underscores

⋅ 2 min read ⋅ Swift

Table of Contents

In Swift, Numeric literals[1] can contain extra formatting that won't affect the underlying value of the literal.

Two extra formattings are:

  1. Underscores (_)
  2. Leading zeros

Here are some examples.

let paddedDouble = 000123.456 // 123.456
let oneMillion = 1_000_000 // 1000000
let justOverOneMillion = 1_000_000.000_000_1 // 10000000.0000001

Extra formatting has no affects on the underlying value

As mentioned earlier, those formatting won't affect the underlying value.

In this example, we declare two integer types with one million values.

let oneMillion = 1000000
let alsoOneMillion = 1_000_000

// 1
oneMillion == alsoOneMillion
// true

// 2
print(oneMillion + alsoOneMillion)
// "2000000\n"

// 3
print("One million: \(alsoOneMillion)")
// "One million: 1000000\n"

1 1000000 is equals to 1_000_000.
2 We can use it like a normal number.
3 It prints out the same way as a normal number.

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

You can apply underscores wherever you want

You probably see underscores in place of the thousands separator characters, but there is no restriction on the position of underscore placement.

You can use it wherever you want.

These are all valid integer declarations.

let oneMillion       = 1000000
let weirdOneMillion = 100_00_00
let weirdOneMillion2 = 10__00000

oneMillion == weirdOneMillion
// true

oneMillion == weirdOneMillion2
// true

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

Benefits

These extra formattings' sole purpose is to improve the readability of the numeric literals.

It can improve readability in many areas as follow.

  1. Large number.
  2. Number with unique format and separator.
  3. Better number alignment.

Large number

Using underscore where the thousands separator characters are expected will improve the readability of a large number.

let oneMillion = 1_000_000 // 1000000
let justOverOneMillion = 1_000_000.000_000_1 // 10000000.0000001

Since it is easier to read, it is harder to get this wrong.

You might find this formatting useful for tests.

Here is an example from Apple where an underscore is used to make a number of iterations clearer (1_000).

func testRange(_ range: Range<T>, iterations: Int = 1_000) {
var integerSet: Set<T> = []
for _ in 0 ..< iterations {
let random = T.random(in: range)
expectTrue(range.contains(random))
integerSet.insert(random)
}
expectEqual(integerSet, Set(range))
}

Number with unique format and separator

Some numbers might have a unique way to read, e.g., hex number.

Here is an example of using an underscore to improve the readability of a hex number.

let red = 0xff0000
let alsoRed = 0xff_00_00

Alignment

Leading zeros can improve readability when used with decimal numbers.

Different in whole numbers and fractional parts might lead to errors.

let aWeight = 0.123
let bWeight = 12345

Using leading zero padded might make it easier to read.

let aWeight = 00000.123
let bWeight = 12345.000

  1. https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html#ID323. ↩︎


Read more article about Swift or see all available topic

Enjoy the read?

If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.

Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.

If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.

Become a patron Buy me a coffee Tweet Share
Previous
Move the most recent commits to a new branch with git

Sometimes I forget to create a new branch for a new feature and commit changes directly to the develop branch. Let's learn one way to correct this mistake in git.

Next
How to change a navigation bar color in SwiftUI on iOS 16

A navigation bar is an essential part of iOS apps. It is a part where we usually apply color to match our branding. In iOS 16, we can customize a navigation bar purely in SwiftUI.

← Home