gpt4 book ai didi

ios - UITextfield 验证 1 - 100 之间的国家/地区货币

转载 作者:行者123 更新时间:2023-11-29 06:02:50 30 4
gpt4 key购买 nike

如何验证 UITextfield 接受 1 到 100 种货币值(最多 2 位小数值)。

有效。0.25 99.99, 1.99,100.00。其他国家货币小数点也表示。0,2599,991,99100,00

无效00.25、100.25、0000000.00、0.125、9.567

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
numberFormatter.locale = [NSLocale currentLocale];// this ensures the right separator behavior
numberFormatter.numberStyle = NSNumberFormatterCurrencyStyle;
numberFormatter.usesGroupingSeparator = YES;
numberFormatter.usesSignificantDigits = YES;
numberFormatter.currencyGroupingSeparator = numberFormatter.locale.groupingSeparator;
numberFormatter.decimalSeparator = numberFormatter.locale.decimalSeparator;

NSString *expression = @"^([0-9]+)?(\\.,([0-9]{1,2})?)?$";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression
options:NSRegularExpressionCaseInsensitive error:nil];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:newString
options:0 range:NSMakeRange(0, [newString length])];

if (numberOfMatches == 0)
return NO;

无法正常工作,无法按 UITextfield 内的 , 。瑞典货币小数点表示“,”而不是“.”我想要工作计算两个输入的值,一个是 int,另一个值是 float

最佳答案

在 UItTxtfield 委托(delegate)方法中尝试此代码。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

NSInteger Currency = [textField.text integerValue];
if(Currency >= 100)
{
return NO;
}
else
{
NSString *enteredCurrency=textField.text;
if([enteredCurrency containsString:@"."])
{
NSArray *array=[enteredCurrency componentsSeparatedByString:@"."];
if([array[1] length] >= 2 || [array[0] length] >= 2)
{
return NO;
}
else
{
return YES;
}
}
else
{
return YES;
}
}
}

关于ios - UITextfield 验证 1 - 100 之间的国家/地区货币,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51379139/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com