Reputation: 1950
I have an application that user must insert his number and send it to the server. The problem that i am encountering is that some users insert their numbers in their native language(For example Urdu, arabic, Indian or others) What I want is to convert all numeric numbers from different languages to English numbers(1,2,3...) and then send it to server. Is there a possible way to achieve that?
Thank you in advance.
Upvotes: 0
Views: 744
Reputation: 77661
I'd be surprised if you can't just do...
NSInteger blah = [enteredString intValue];
// you will have to know if it's an int, float, double, etc...
// the entered number is still a number just using a different font (I guess).
// the shape of the number 2 comes entirely from the font so I don't see why this wouldn't work
But if that doesn't work take a look at the NSNumberFormatter
class. You should be able to do something like...
NSNumberFormatter *nf = [NSNumberFormatter new];
NSNumber *number = [nf numberFromString:enteredString];
Either way should work. Try the first one first. If that doesn't work then give the number formatter a go. You may have to set the locale of the number formatter.
Tested with a working project
// This is the only code.
#import "ViewController.h"
@interface ViewController () <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *textField;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self getNumber];
return YES;
}
- (void)getNumber
{
NSInteger number = [self.textField.text intValue];
NSLog(@"%ld", (long)number);
}
@end
I changed the simulator language to Arabic and it worked perfectly.
Screenshot...
Code...
Literal string vs. entered string
I'm guessing this is because your development language is English.
Anyway, when you enter the literal string ١٢٣
into Xcode and store it in a string it is different from taking the string ١٢٣
from an Arabic textfield...
Upvotes: 2