Created
March 8, 2022 05:12
-
-
Save wiwi-git/646b628dec8a7ed940c3d0cc885017fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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