0

I have number 36381129. I need number 36.381,129

I tried this code, but it doesn't work.

int number = 36381129;
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];            
[numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
NSString *numberAsString = [numberFormatter stringFromNumber: [NSNumber numberWithInt:number]];

I give this number.

36.381.129,00 $

Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
Ales
  • 13
  • 5

3 Answers3

0

I think this is BRAZILIAN REAL CURRENCY Format. You have to call this method with your price in float value, and this method returns your string into your format. Like if we pass 123456789, then it will return 123,456,789.00.

//Convert Price to Your Price Format
+(NSString*)convertFormat:(float)value{

    NSString * convertedString = [NSString stringWithFormat:@"%.2f", value];

    NSString * leftPart;
    NSString * rightPart;

    if (([convertedString rangeOfString:@"."].location != NSNotFound)) {
        rightPart = [[convertedString componentsSeparatedByString:@"."] objectAtIndex:1];
        leftPart = [[convertedString componentsSeparatedByString:@"."] objectAtIndex:0];
    }

    //NSLog(@"%d",[leftPart length]);
    NSMutableString *mu = [NSMutableString stringWithString:leftPart];

    if ([mu length] > 3) {
        [mu insertString:@"." atIndex:[mu length] - 3];
        //NSLog(@"String is %@ and length is %d", mu, [mu length]);
    }

    for (int i=7; i<[mu length]; i=i+4) {
        [mu insertString:@"." atIndex:[mu length] - i];
        //NSLog(@"%d",mu.length);
    }

    convertedString = [[mu stringByAppendingString:@","] stringByAppendingString:rightPart];

    return convertedString;

}

For more details, refer this blog.

Hope, this is what you're looking for. Any concern get back to me.

Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
-1

Welcome to SO. Your question is pretty vague.

Currency formats depend on the user's locale. It's generally better to either use the default locale of the device, or set a locale, and then let the currency formatter create that string that's appropriate for that locale.

If you set up a hard-coded currency format then it will be wrong for some users. (For example in the US we use a "." as a decimal separator and commas as a grouping symbol. In most of Europe they use a comma as a decimal separator and the period as a grouping symbol. Some countries put the currency symbol at the end of a currency amount, and others put it at the beginning.)

Duncan C
  • 128,072
  • 22
  • 173
  • 272
-1

You can use this code:

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
NSString *groupingSeparator = [[NSLocale currentLocale] objectForKey:NSLocaleGroupingSeparator];
[formatter setGroupingSeparator:groupingSeparator];
[formatter setGroupingSize:3];
[formatter setAlwaysShowsDecimalSeparator:NO];
[formatter setUsesGroupingSeparator:YES];

and use it this way:

NSString *formattedString = [formatter stringFromNumber:[NSNumber numberWithFloat:rev];

This is a generic solution and will work for any country according to their grouping separator

Taken from: https://stackoverflow.com/a/5407103/2082569

Community
  • 1
  • 1
atulkhatri
  • 10,896
  • 3
  • 53
  • 89
  • Why not set the `locale` property instead of the `groupingSeparator`? – johnnieb Dec 11 '15 at 19:17
  • I am using NSLocale to get country specific groupingSeparator – atulkhatri Dec 11 '15 at 19:27
  • Would the result be different if I just set the `locale` instead of the `groupingSeparator`? – johnnieb Dec 11 '15 at 19:41
  • There is no need to set the locale or the grouping separator. Both default to the current locale. – rmaddy Dec 11 '15 at 20:15
  • @rmaddy If you're formatting outside of current locale, I think you would need to do one or other. – johnnieb Dec 11 '15 at 20:28
  • But the OP is formatting in their own locale. None of the current answers address the question being asked. The OP is mistakenly trying to format an integer value but they actually want a decimal value with 3 decimal places. – rmaddy Dec 11 '15 at 20:30