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.

## 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.654String(format: "%f", value) // 123.654000``

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

%.`NumberOfDigits`f

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

``let value = 123.654String(format: "%.0f", value) // 124String(format: "%.1f", value) // 123.7String(format: "%.2f", value) // 123.65String(format: "%.3f", value) // 123.654String(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.456123,456``

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

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

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

Sponsor sarunw.com and reach thousands of iOS developers.

## 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 = 0nf.string(from: 123.654) // "124" equavalent to "%.0f"nf.minimumFractionDigits = 1nf.string(from: 123.654) // "123.7" equavalent to "%.1f"nf.minimumFractionDigits = 2nf.string(from: 123.654) // "123.65" equavalent to "%.2f"nf.minimumFractionDigits = 3nf.string(from: 123.654) // "123.654" equavalent to "%.3f"nf.minimumFractionDigits = 4nf.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 = 0nf.string(from: 123.654) // "124"nf.maximumFractionDigits = 1nf.string(from: 123.654) // "123.7"nf.maximumFractionDigits = 2nf.string(from: 123.654) // "123.65"nf.maximumFractionDigits = 3nf.string(from: 123.654) // "123.654"nf.maximumFractionDigits = 4nf.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.

Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.

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.

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