Tuesday, December 15, 2015

How to Convert Price to Global Currency Format in iOS


It can handle all your numeric formatting needs and do so in an automatically localized fashion, when used correctly (since currencies around the world are often written with different punctuation & formatting).
In particular, check out the number formatting guide linked to at the top of the class documentation.
Using NSNumberFormatter:
NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
NSString *numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithFloat: 1000.99]];
NSLog(@"%@", numberAsString);

------------- OR ----------------
Convert Price to Global Currency Format

+(NSString*)convertGlobalCurrencyFormat:(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);
    }
    
    //    if ([mu length] > 3) {
    //        [mu insertString:@"," atIndex:[mu length] - 3];
    //        //NSLog(@"String is %@ and length is %d", mu, [mu length]);
    //    }
    //    if ([mu length] > 6) {
    //        [mu insertString:@"," atIndex:[mu length] - 6];
    //        //NSLog(@"String is %@ and length is %d", mu, [mu length]);
    //    }
    //    if ([mu length] > 9) {
    //        [mu insertString:@"," atIndex:[mu length] - 9];
    //        //NSLog(@"String is %@ and length is %d", mu, [mu length]);
    //    }
    
    convertedString = [[mu stringByAppendingString:@"."stringByAppendingString:rightPart];
    
    return convertedString;

}


Convert Price to Brizilian Real Format

+(NSString*)convertBrasilianRealFormat:(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);
    }
    
    //    if ([mu length] > 3) {
    //        [mu insertString:@"." atIndex:[mu length] - 3];
    //        //NSLog(@"String is %@ and length is %d", mu, [mu length]);
    //    }
    //    if ([mu length] > 6) {
    //        [mu insertString:@"." atIndex:[mu length] - 6];
    //        //NSLog(@"String is %@ and length is %d", mu, [mu length]);
    //    }
    //    if ([mu length] > 9) {
    //        [mu insertString:@"." atIndex:[mu length] - 9];
    //        //NSLog(@"String is %@ and length is %d", mu, [mu length]);
    //    }
    
    convertedString = [[mu stringByAppendingString:@","]stringByAppendingString:rightPart];
    
    return convertedString;

}

No comments:

Post a Comment