I am new to swift and am trying to write an application - calculator. Maybe my question may seem silly, but still

When creating a number with a dot, I want the line to display a number with "," (something like 3,14 ), but swift (using the Double type) by default displays the number as 3.14 .
Can this be changed? Is it necessary to resort to NumberFormatter() and if so, how to use it correctly?

Thank you in advance

1 answer 1

Option 1 - Using NumberFormatter

 let pi: Double = 3.14 let numFormatter = NumberFormatter() numFormatter.numberStyle = .decimal // устанавливаем стиль - десятичный numFormatter.decimalSeparator = "," // устанавливаем десятичный разделитель let piStr = numFormatter.string(from: NSNumber(value: pi))! print(piStr) // 3,14 

Option 2 - Using character substitution in string

 let pi: Double = 3.14 var piStr = String(pi) piStr = piStr.replacingOccurrences(of: ".", with: ",") // заменяем символ '.' на ',' print(piStr) // 3,14