2

I want to convert numbers of any languages to english format in offline mode It means if I am getting 10.00 in Arabic as "١٠.٠٠" . I want to convert it back to 10.00.

I found some links but I don’t think this is a good idea to do like this in this case. Because for this I have to write for all languages. convert kABPersonPhoneProperty to arabic numbers

Can anyone please help me in solving this

Community
  • 1
  • 1
Prajnaranjan Das
  • 1,378
  • 1
  • 9
  • 26
  • try this http://stackoverflow.com/questions/11670330/how-to-display-arabic-numbers-inside-iphone-apps-without-localizing-each-digit ;) – Nathan Hegedus May 22 '14 at 13:39
  • Hello Nathan Hegedus, Thanks for your answer. I tried this but its retuning NAN if I am converting from arabic numbers to english I used 10 as @"١٠” and locale as @“en_US” – Prajnaranjan Das May 23 '14 at 06:50

1 Answers1

2

I solved in this way

BOOL isCommaUsingCountry = [self ifCountryCodePresentIntheList];

NSString *strAfterReplacing = @"";
if (isCommaUsingCountry)
{
    //If some numbers come like “10.000,00”
    NSString *strAfterReplacingDot = [str stringByReplacingOccurrencesOfString:@"." withString:@""];
    strAfterReplacing = [strAfterReplacingDot  stringByReplacingOccurrencesOfString:@"," withString:@"."];

    //For removing Arabic comma
    strAfterReplacing = [strAfterReplacing  stringByReplacingOccurrencesOfString:@"٫" withString:@"."];

}
else
{
    strAfterReplacing = [str stringByReplacingOccurrencesOfString:@"," withString:@""];
}

NSArray *separray = [strAfterReplacing componentsSeparatedByString:@"."];
NSString *numbersBeforeDecimal = @"";
NSString *numbersAfterDecimal = @"";
if([separray count] == 2)
{
    numbersBeforeDecimal = [separray objectAtIndex:0];
    numbersAfterDecimal = [separray objectAtIndex:1];
}

//Converting back to US format
if([strAfterReplacing length] > 0)
{
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    NSLocale *usLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];
    [formatter setLocale:usLocale];
    NSNumber *newNum1  = [formatter numberFromString:numbersBeforeDecimal];
    NSNumber *newNum2  = [formatter numberFromString:numbersAfterDecimal];
    numbersBeforeDecimal = [NSString stringWithFormat:@"%@", newNum1];
    numbersAfterDecimal = [NSString stringWithFormat:@"%@", newNum2];
}
NSString *combinedStr = [NSString stringWithFormat:@"%@.%@", numbersBeforeDecimal, numbersAfterDecimal];
Prajnaranjan Das
  • 1,378
  • 1
  • 9
  • 26