5

How to specify fractional digits for formatted number string in Swift

 3 years ago
source link: https://sarunw.com/posts/how-to-specify-fractional-digits-for-formatted-number-string-in-swift/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to specify fractional digits for formatted number string in Swift


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.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"
Practical Sign in with Apple

Learn everything you need to know about Sign in with Apple to be able to integrate it in your existing app or a new one.

Get it now

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.

Practical Sign in with Apple

Learn everything you need to know about Sign in with Apple to be able to integrate it in your existing app or a new one.

Get it now

Related Resources #


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

Tweet

Share

Previous
How to use DateFormatter in Swift

Learn how to use this expensive DateFormatter.

Related Posts

Browse all content by tag

Sign in with Apple Tutorial, Part 3: Backend – Token verification

Part 3 in a series Sign in with Apple. In this part, we will see how backend can use the token to sign up/sign in users.

Swift
How to Add inline images with text in SwiftUI

In iOS 14, we have a new way to put images along with texts.

String
Reduce boilerplate code with an automatic synthesis of Equatable and Hashable conformance

Equatable and Hashable are two essential protocols in the Swift world. Let's learn an old Swift feature that you might forget.

Swift

Get new posts weekly

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 — entirely for free.

← Home


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK