Viktor
Viktor

Reputation: 1225

Swift. How to set custom number format? With space thouthand separator and two digits after point

I need separate thouthands with space, and two digit after point.

let d1: Double = 20000000.0
let d2: Double = 1.2345

I want to view:

let s1 = String(format: "????", d1) //20 000 000.00
let s2 = String(format: "????", d2) //1.23

How to do it?

Upvotes: -1

Views: 1233

Answers (1)

rraphael
rraphael

Reputation: 11057

let d1: Double = 20000000.0
let d2: Double = 1.2345

let formatter = NumberFormatter()
formatter.groupingSeparator = " "
formatter.numberStyle = .decimal
formatter.minimumFractionDigits = 2
formatter.maximumFractionDigits = 2
formatter.decimalSeparator = "." // Default separator is dependent to the current local.

print(formatter.string(for: d1)) // 20 000 000.00
print(formatter.string(for: d2)) // 1.23

Upvotes: 5

Related Questions