 
         let _start: UITextPosition?
         if wasTextDeleted {
-            _start = position(from: endOfDocument, offset: -numCharactersEncountered+string.lengthOfRange(range))
+            _start = position(from: endOfDocument, offset: -numCharactersEncountered+currentText.lengthOfRange(range))
         } else {
             _start = position(from: beginningOfDocument, offset: numCharactersEncountered)
         }

        // Remove all non-digits or periods from the replacement string.
        let string = digitExpression.stringByReplacingMatches(in: string, options: [], range: string.range, withTemplate: "")

        let wasTextDeleted = string.length < string.lengthOfRange(range)
        let numDigits: Int
        if currentText.length > 0 {
            if wasTextDeleted {
                numDigits = currentText.charactersInSetAfterIndex(characterSet, index: string.characters.index(after: range.lowerBound)).count
            } else {
                let numDigitsInEnteredString = string.charactersInSet(CharacterSet.decimalDigits).count
                numDigits = currentText.charactersInSetBeforeIndex(characterSet, index: range.lowerBound).count + numDigitsInEnteredString

        // If characters are removed, edit the range to make sure to ignore any non-digits (e.g., ',').
        var range = range
        if wasTextDeleted {
            let length = currentText.lengthOfRange(range)
            while currentText.substring(with: range).charactersInSet(CharacterSet.decimalDigits).count < length {
                if range.lowerBound == currentText.startIndex {
                    break
                }

                let startIndex = currentText.characters.index(before: range.lowerBound)
                range = startIndex..<range.upperBound
            }
        }
