Skip to content

Instantly share code, notes, and snippets.

@wiwi-git
Created March 8, 2022 05:12
Show Gist options
  • Save wiwi-git/646b628dec8a7ed940c3d0cc885017fd to your computer and use it in GitHub Desktop.
Save wiwi-git/646b628dec8a7ed940c3d0cc885017fd to your computer and use it in GitHub Desktop.
func moneyPlaceHolder(money: Int) -> String {
guard money >= 0 else { return "" }
let moneyString = String(money)
let moneyCount = moneyString.count
let insertPlaceCount = 3
if moneyCount <= insertPlaceCount {
return moneyString
}
let index0: String.Index = moneyString.startIndex
var resultString = ""
var loopIndex = 0
for i in (0 ..< moneyCount).reversed() {
resultString += String(moneyString[moneyString.index(index0, offsetBy: i)])
loopIndex += 1
if loopIndex >= insertPlaceCount {
resultString += ","
loopIndex = 0
}
}
return String(resultString.reversed())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment