Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //no exponent
- //6. or .4 are valid
- // only + or - are not valid
- // +. are not valid
- import Foundation
- func validDouble(_ s:String) -> Bool{
- guard s.count > 0 else { return false }
- let arr = Array(s.trimmingCharacters(in: .whitespaces))
- var foundDot = false
- var i = 0
- var hasValue = false
- while i < arr.count {
- let char = arr[i]
- if i == 0 && (char == "+" || char == "-"){
- i += 1
- continue
- }
- if char == "." {
- if foundDot { return false}
- foundDot = true
- i += 1
- continue
- }
- if char < "0" || char > "9" { return false}
- hasValue = true
- i += 1
- }
- return hasValue
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement