OStack程序员社区-中国程序员成长平台

标题: ios - 将任何语言的数字转换为特定语言 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 22:18
标题: ios - 将任何语言的数字转换为特定语言

我想在离线模式下将任何语言的数字转换为英文格式 这意味着如果我在阿拉伯语中得到 10.00 为 "١٠.٠٠"。我想将其转换回 10.00。

我找到了一些链接,但我认为在这种情况下这样做不是一个好主意。因为为此我必须为所有语言写作。 convert kABPersonPhoneProperty to arabic numbers

谁能帮我解决这个问题



Best Answer-推荐答案


我是这样解决的

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];

关于ios - 将任何语言的数字转换为特定语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23808228/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4