Created
October 20, 2015 12:38
-
-
Save zigdanis/2f735f34fb44b38d83b5 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
+ (NSNumberFormatter *)numberFormatter { | |
static NSNumberFormatter *_numberFormatter = nil; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
_numberFormatter = [[NSNumberFormatter alloc] init]; | |
[_numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; | |
[_numberFormatter setLocale:[NSLocale currentLocale]]; | |
_numberFormatter.usesGroupingSeparator = NO; | |
_numberFormatter.maximumFractionDigits = 1; | |
}); | |
return _numberFormatter; | |
} | |
- (NSNumber *)amountValueFromString:(NSString *)string { | |
NSNumberFormatter *numberFormatter = [AppHelper numberFormatter]; | |
NSNumber *numberValue = [numberFormatter numberFromString:string]; | |
return numberValue; | |
} | |
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { | |
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string]; | |
NSNumber *amountValue = [self amountValueFromString:newString]; | |
double fractionPart = [amountValue doubleValue] - floor([amountValue doubleValue]); | |
BOOL fractionPartTooPrecise = fractionPart != 0 && fractionPart < 0.1; | |
... | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment