Skip to content

Instantly share code, notes, and snippets.

@zigdanis
Created October 20, 2015 12:38
Show Gist options
  • Save zigdanis/2f735f34fb44b38d83b5 to your computer and use it in GitHub Desktop.
Save zigdanis/2f735f34fb44b38d83b5 to your computer and use it in GitHub Desktop.
+ (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