Change comma( , ) with dot ( . ) in SwiftUI

Hello
I'm making an app that displays large Double numbers, for example 86,859.002. The problem is how do I change the comma( , ) with the dot ( . )

Thank you
Answered by OOPer in 654236022
When you show numeric values for users, you use NumberFormatter:
Code Block
let value: Double = 86_859.002
let nf = NumberFormatter()
nf.numberStyle = .decimal
nf.thousandSeparator = "."
nf.decimalSeparator = ","
nf.maximumFractionDigits = 3
print(nf.string(from: value as NSNumber) ?? "")
//->86.859,002


You usually set an appropriate Locale instead of specifying thousandSeparator explicitly.
An example.
Code Block
let nfEs = NumberFormatter()
nfEs.numberStyle = .decimal
nfEs.locale = Locale(identifier: "es_ES")
nfEs.maximumFractionDigits = 3
print(nfEs.string(from: value as NSNumber) ?? "")
//->86.859,002


Accepted Answer
When you show numeric values for users, you use NumberFormatter:
Code Block
let value: Double = 86_859.002
let nf = NumberFormatter()
nf.numberStyle = .decimal
nf.thousandSeparator = "."
nf.decimalSeparator = ","
nf.maximumFractionDigits = 3
print(nf.string(from: value as NSNumber) ?? "")
//->86.859,002


You usually set an appropriate Locale instead of specifying thousandSeparator explicitly.
An example.
Code Block
let nfEs = NumberFormatter()
nfEs.numberStyle = .decimal
nfEs.locale = Locale(identifier: "es_ES")
nfEs.maximumFractionDigits = 3
print(nfEs.string(from: value as NSNumber) ?? "")
//->86.859,002


Change comma( , ) with dot ( . ) in SwiftUI
 
 
Q