Guest User

Untitled

a guest
Nov 17th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. func convert(_ textValue: String) -> Int{
  2.  
  3. var total = 0
  4. var valueMap = [
  5. "1" as Character: 1,
  6. "2": 2,
  7. "3": 3,
  8. "4": 4,
  9. "5": 5,
  10. "6": 6,
  11. "7": 7,
  12. "8": 8,
  13. "9": 9,
  14. "0": 0
  15. ]
  16. for (i, c) in textValue.enumerated() {
  17. let exponent = textValue.count - i - 1
  18. if let value = valueMap[c] {
  19. let num = Decimal(value) * pow(10, exponent)
  20. total += NSDecimalNumber(decimal: num).intValue
  21. }
  22. }
  23. return total
  24. }
Add Comment
Please, Sign In to add comment