How to specify fractional digits for formatted number string in Swift

⋅ 4 min read ⋅ Swift String

Table of Contents

Fractional digits are a number of digits after the decimal separator (.). When you want to present a floating or a double value, you might want to control how many digits you want. Otherwise, your UI will look inconsistent.

There are a few ways to format a Float or a Double String in Swift.

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

Sponsor sarunw.com and reach thousands of iOS developers.

String format with %f

The first one is String(format:_:) with floating string format specifier %f.

The default fractional digits of %f is six.

let value = 123.654
String(format: "%f", value) // 123.654000

You can control the fraction digits by putting the desired number prefix by . and put it after %.

%.NumberOfDigitsf

Here's are example of setting fraction digits to 0, 1, 2, 3, 4 respectively.

let value = 123.654
String(format: "%.0f", value) // 124
String(format: "%.1f", value) // 123.7
String(format: "%.2f", value) // 123.65
String(format: "%.3f", value) // 123.654
String(format: "%.4f", value) // 123.6540

Swift will automatically round the number for you. If you want greater control over this, you need to use NumberFormatter, which we will discuss later.

I want to highlight here that String(format:_:) doesn't take locale into consideration. You might not know that comma (,) is used as a decimal separator in some parts of the world. Same for thousand separators, which can vary from space, period, and comma.

Both a comma and a period are a valid decimal separator.

123.456
123,456

To make String(format:_:) respect locale, you better use init(format:locale:_:).

let value = 123.654
String(format: "%.2f", locale: Locale(identifier: "de"), value) // "123,65"

NumberFormatter

If you want full control over fractional digits and rounding behavior, NumberFormatter is the advanced option over string format.

Minimum fraction digits.
The minimum number of digits after the decimal separator. This is equivalent property as what we define in %f.

let nf = NumberFormatter()

nf.minimumFractionDigits = 0
nf.string(from: 123.654) // "124" equavalent to "%.0f"

nf.minimumFractionDigits = 1
nf.string(from: 123.654) // "123.7" equavalent to "%.1f"

nf.minimumFractionDigits = 2
nf.string(from: 123.654) // "123.65" equavalent to "%.2f"

nf.minimumFractionDigits = 3
nf.string(from: 123.654) // "123.654" equavalent to "%.3f"

nf.minimumFractionDigits = 4
nf.string(from: 123.654) // "123.6540" equavalent to "%.4f"

Maximum fraction digits.
The maximum number of digits after the decimal separator. The difference between maximum and minimum fraction digits is maximum fraction digits won't add a padding zero at the end.

let nf = NumberFormatter()

nf.string(from: 123.654) // "124"

nf.maximumFractionDigits = 0
nf.string(from: 123.654) // "124"

nf.maximumFractionDigits = 1
nf.string(from: 123.654) // "123.7"

nf.maximumFractionDigits = 2
nf.string(from: 123.654) // "123.65"

nf.maximumFractionDigits = 3
nf.string(from: 123.654) // "123.654"

nf.maximumFractionDigits = 4
nf.string(from: 123.654) // "123.654"

Rounding mode.
NumberFormatter uses halfEven rounding mode, which round towards the nearest integer, or towards an even number if equidistant.

let nf = NumberFormatter()
nf.string(from: 123.645) // 123.64

If this isn't a behavior that you want, you have seven modes to choose from:

Rounding Mode Description
ceiling Round towards positive infinity.
floor Round towards negative infinity.
down Round towards zero.
up Round away from zero.
halfEven Round towards the nearest integer, or towards an even number if equidistant.
halfDown Round towards the nearest integer, or towards zero if equidistant.
halfUp Round towards the nearest integer, or away from zero if equidistant.

Conclusion

String format specifier (%f) is a fast and simple way of format fraction digits, while NumberFormatter offer you a lot of options but demanding more processing resources. It's up to you to pick the right one for your needs.

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

Sponsor sarunw.com and reach thousands of iOS developers.
Read more article about Swift, String, 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
How to use DateFormatter in Swift

Learn how to use this expensive DateFormatter.

Next
Different ways to sort an array of strings in Swift

Learn a proper way to sort an array of strings in each circumstance.

← Home