Iphone “如何使用方法”;小数分隔符“;来自NSNumberFormatter

Iphone “如何使用方法”;小数分隔符“;来自NSNumberFormatter,iphone,ios,Iphone,Ios,在我的应用程序中,我尝试根据本地设置检查用户是否输入了右小数点分隔符(如果用户出错,该部分数据不会进入CoreData数据库): 我获得用户输入: NSString *firstForDistance = _forDistanceTextField.text; 尝试从字符串中提取小数分隔符: NSString *separator = [firstForDistance decimalSeparator]; 然后检查分隔符是否符合用户的本地设置或他是否输入了错别字。如果输入错误或出错,我会将

在我的应用程序中,我尝试根据本地设置检查用户是否输入了右小数点分隔符(如果用户出错,该部分数据不会进入CoreData数据库):

我获得用户输入:

NSString *firstForDistance = _forDistanceTextField.text;
尝试从字符串中提取小数分隔符:

NSString *separator = [firstForDistance decimalSeparator];
然后检查分隔符是否符合用户的本地设置或他是否输入了错别字。如果输入错误或出错,我会将其替换为本地设置中的十进制分隔符:

if (separator != [locale objectForKey:NSLocaleDecimalSeparator]) {
    firstForDistance = [firstForDistance stringByReplacingOccurrencesOfString:separator withString:[locale objectForKey: NSLocaleDecimalSeparator]];
}
代码的其余部分:

self.user.forDistance = [numberFormatter numberFromString:firstForDistance];

[self.delegate usersMakeViewController:self addUser:self.user];
}
这很好,也很容易,但提取不起作用:

NSString *separator = [firstForDistance decimalSeparator];
方法“decimalSeparator”是来自NSNumberFormatter的实例方法,我从Xcode得到错误


如何解决此问题?

您可以像下面这样检查字符串是否有小数分隔符

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    [numberFormatter setLocale:[NSLocale currentLocale]];
    if ([textField.text rangeOfString:numberFormatter.decimalSeparator].location != NSNotFound) 
        NSLog(@"YES,  Give number has decimalSeparator");
    else
        NSLog(@"NO, dont have decimalSeparator");

您可以像下面这样检查字符串是否有小数分隔符

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    [numberFormatter setLocale:[NSLocale currentLocale]];
    if ([textField.text rangeOfString:numberFormatter.decimalSeparator].location != NSNotFound) 
        NSLog(@"YES,  Give number has decimalSeparator");
    else
        NSLog(@"NO, dont have decimalSeparator");
修正如下:

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];

NSLocale *locale = [NSLocale currentLocale];
NSString *firstForDistance = _forDistanceTextField.text;

firstForDistance = [firstForDistance stringByReplacingOccurrencesOfString:@"." withString:[locale objectForKey: NSLocaleDecimalSeparator]];
firstForDistance = [firstForDistance stringByReplacingOccurrencesOfString:@"," withString:[locale objectForKey: NSLocaleDecimalSeparator]];

self.user.forDistance = [numberFormatter numberFromString:firstForDistance];
[self.delegate usersMakeViewController:self addUser:self.user];
希望这对某人有所帮助。

如下所示:

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];

NSLocale *locale = [NSLocale currentLocale];
NSString *firstForDistance = _forDistanceTextField.text;

firstForDistance = [firstForDistance stringByReplacingOccurrencesOfString:@"." withString:[locale objectForKey: NSLocaleDecimalSeparator]];
firstForDistance = [firstForDistance stringByReplacingOccurrencesOfString:@"," withString:[locale objectForKey: NSLocaleDecimalSeparator]];

self.user.forDistance = [numberFormatter numberFromString:firstForDistance];
[self.delegate usersMakeViewController:self addUser:self.user];

希望这有助于某人。

谢谢你,Narayana,这也很方便,我会考虑的,但是我想替换分离器,如果用户根据他的本地设置来打字(昏迷或相反的时间)。@ USE1818438你能解释一下Malthink你@ Narayana吗?这也很方便,我会考虑,但我想更换分离器,如果用户键入(昏迷或相反的时间段),取决于他的本地设置。@ USE1818438你能解释更多1000.75==>1.00 0.75或1000,75?不(嗡嗡声!)1000.75==>1.000.75还是1000,75?不(嗡嗡声!)