Iphone User
Iphone User

Reputation: 1950

Converting a user's phone number into english numbers

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

Answers (1)

Fogmeister
Fogmeister

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... enter image description here

Code... enter image description here

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...

enter image description here

enter image description here

Upvotes: 2

Related Questions