0

我创建了将货币转换为 Word 格式的函数,但我的问题是,当输入 textfeild 值100000时,我需要印度格式十万,但它给了我十万,所以任何解决方案都可以。

 NSString *str1 = txtAmount.text;
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setNumberStyle: NSNumberFormatterSpellOutStyle];

    NSString *localeStr = [appDel.countryCodeDict valueForKey:appDel.selectedCountry];


    if ([appDel.selectedCountry isEqualToString:@"France"]) {
        localeStr = @"fr";
    }
    if ([appDel.selectedCountry isEqualToString:@"Germany"]) {
        localeStr = @"de";
    }


    NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:localeStr];
    [formatter setLocale:usLocale];  

    [formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];

    [formatter setNumberStyle:NSNumberFormatterCurrencyStyle];


    NSArray *valueArr=[str1 componentsSeparatedByString:@"."];


    [formatter setNumberStyle: NSNumberFormatterSpellOutStyle];

    NSString *firstStr = [valueArr objectAtIndex:0];
    NSString *seconfStr = [valueArr lastObject];           

    if (valueArr.count==2 && ![seconfStr isEqualToString:@""]) {
        double firstAmt = [firstStr doubleValue];
        NSString *convertStr = [formatter stringFromNumber:[NSNumber numberWithDouble:firstAmt]];

        double secondAmt = [seconfStr doubleValue];
        NSString *convertStr1 = [formatter stringFromNumber:[NSNumber numberWithDouble:secondAmt]];

        NSString *finalStr = [convertStr stringByAppendingFormat:@" %@ %@ %@",appDel.firstCurrencystr,convertStr1,appDel.secondCurrencystr];
        NSLog(@"%@",finalStr);
        NSString *firstCapChar = [[finalStr substringToIndex:1] capitalizedString];

        tempStr = [finalStr stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:firstCapChar];

    }else{

        double firstAmt = [firstStr doubleValue];
        NSString *convertStr = [formatter stringFromNumber:[NSNumber numberWithDouble:firstAmt]];
        convertStr = [convertStr stringByAppendingFormat:@" %@",appDel.firstCurrencystr];
        NSString *firstCapChar = [[convertStr substringToIndex:1] capitalizedString];
        tempStr = [convertStr stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:firstCapChar];

    }

    appDel.lblAmountView.lblAmount.text = tempStr;

任何想法。

4

2 回答 2

2

不知道您编码了什么,但如果您希望货币采用印度风格,请查看以下代码:-

NSLocale *indiaLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_IN"] autorelease];
double currency = 1000000000.00; 
NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
[numberFormatter setLocale:indiaLocale];

NSString *numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithInt:currency]];
NSString *str2 = [NSString stringWithFormat:@"Converted:%@",numberAsString]; 

NSLog(@"String:%@",str2);
于 2012-08-09T05:49:55.690 回答
2
at last i convert the hole currency in lakh and crore format of india:



-(NSString *)stringfromNumberIndia:(NSString *)str_word
    {
        NSString *tempStr=@"";

        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        [formatter setNumberStyle: NSNumberFormatterSpellOutStyle];

        NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_IN"];
        [formatter setLocale:usLocale];
        [formatter setNumberStyle: NSNumberFormatterSpellOutStyle];


        if ([str_word length]>5) {

            NSString* str_convert = [str_word substringFromIndex:[str_word length]-5];
            NSString* str_convert1 = [str_word substringToIndex:[str_word length]-5];
            NSLog(@"str :%@",str_convert);
            NSLog(@"str1 :%@",str_convert1);
            tempStr = [[self stringfromNumberIndia:str_convert] stringByAppendingString:tempStr];
            int l=[str_convert1 length];
             for (int i=0; i<l;) {

                NSRange range;
                if (l>2 && i == 0) {
                    range=NSMakeRange(l-i-2, 2);
                }
                else{
                    range=NSMakeRange(0, l-i);
                }
                NSString *str_first=[str_convert1 substringWithRange:range];
                 if ([str_first intValue] == 0) {
                     if (i == 0)
                         i+=2;
                     else
                         break;
                     continue;
                }
                NSLog(@"str_first:%@",str_first);
                if (i== 0) {
                    str_first=[formatter stringFromNumber:[NSNumber numberWithDouble:[str_first doubleValue]]];
                    str_first = [str_first stringByAppendingFormat:@" lakh "];
                    tempStr = [str_first stringByAppendingString:tempStr];
                }
                else {
                    str_first =[self stringfromNumberIndia:str_first];
                    str_first = [str_first stringByAppendingFormat:@" crore "];
                    tempStr = [str_first stringByAppendingString:tempStr];
                    break;
                }
                 i+=2;
            }
        }
        else if ([str_word intValue] !=0){
            double Amt = [str_word doubleValue];
            NSString *convertStr = [formatter stringFromNumber:[NSNumber numberWithDouble:Amt]];

            NSString *firstCapChar = [convertStr substringToIndex:1];
            tempStr = [convertStr stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:firstCapChar];
        }
        return  tempStr;

    }

谢谢。

于 2012-08-13T14:07:10.930 回答