I need to create a function to add the decimal and thousands separators to a number that is a string, I know I could use numberFormatter but I want to practice another way of doing it. For example if I have "454685" the result will be "$ 4,546.85". This is my code:
let number = "111234567"
let firstIndex = number.startIndex
let firstNumber = number[firstIndex]
let lastIndex = number.index(before: number.endIndex)
let lastNumber = String(number[lastIndex])
let beforeLastIndex = number.index(number.endIndex, offsetBy: -2)
let beforeLastNumber = String(number[beforeLastIndex])
var decimalPart = beforeLastNumber + lastNumber
let decimalindex = number.index(number.endIndex, offsetBy: -2)
let intPart = String(number[..<decimalindex])
let totalNumber = "$" + intPart + "," + decimalPart
print(totalNumber)
var thousandSeparators = intPart.count % 3 == 1
I'm stuck here, I separated the decimal part from the integer part, but then I don't know how to add the thousand separator "." to string number.