Configuring Formatter Behavior and Style

- setFormatterBehavior:

Sets the formatter behavior of the receiver.

Example

NSNumber *number = [NSNumber numberWithFloat:23.034];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehaviorDefault];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSLog(@"%@", [numberFormatter stringFromNumber:number]);
		

Output

2014-04-16 05:42:15.600 iOS-Tutorial[1276:a0b] 23.034
		

- formatterBehavior

Returns an NSNumberFormatterBehavior constant that indicates the formatter behavior of the receiver.

Example

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehaviorDefault];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSLog(@"%d", [numberFormatter formatterBehavior]);
		

Output

2014-04-16 05:43:11.704 iOS-Tutorial[1300:a0b] 1040
		

+ setDefaultFormatterBehavior:

Sets the default formatter behavior for new instances of NSNumberFormatter .

Example

NSNumber *number = [NSNumber numberWithFloat:23.034];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
[numberFormatter setFormatterBehavior:[NSNumberFormatter defaultFormatterBehavior]];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSLog(@"%d", [numberFormatter formatterBehavior]);
		

Output

2014-04-16 05:45:17.075 iOS-Tutorial[1312:a0b] 1040
		

+ defaultFormatterBehavior

Returns an NSNumberFormatterBehavior constant that indicates default formatter behavior for new instances of NSNumberFormatter.

Example

NSNumber *number = [NSNumber numberWithFloat:23.034];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
[numberFormatter setFormatterBehavior:[NSNumberFormatter defaultFormatterBehavior]];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSLog(@"%d", [numberFormatter formatterBehavior]);
		

Output

2014-04-16 05:45:17.075 iOS-Tutorial[1312:a0b] 1040
		

- setNumberStyle:

Sets the number style used by the receiver.

Example

NSNumber *number = [NSNumber numberWithFloat:23.034];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehaviorDefault];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSLog(@"%@", [numberFormatter stringFromNumber:number]);
		

Output

2014-04-16 05:42:15.600 iOS-Tutorial[1276:a0b] 23.034
		

- numberStyle

Returns the number-formatter style of the receiver.

Example

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehaviorDefault];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSLog(@"%u", [numberFormatter numberStyle]);
		

Output

2014-04-16 05:47:38.007 iOS-Tutorial[1333:a0b] 1
		

- setGeneratesDecimalNumbers:

Controls whether the receiver creates instances of NSDecimalNumber when it converts strings to number objects.

Example

NSNumber *number = [NSNumber numberWithFloat:23.034];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehaviorDefault];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setGeneratesDecimalNumbers:YES];
NSLog(@"%@", [numberFormatter stringFromNumber:number]);
		

Output

2014-04-16 05:49:05.254 iOS-Tutorial[1348:a0b] 23.034
		

- generatesDecimalNumbers

Returns a Boolean value that indicates whether the receiver creates instances of NSDecimalNumber when it converts strings to number objects.

Example

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
    [numberFormatter setFormatterBehavior:NSNumberFormatterBehaviorDefault];
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    BOOL status = [numberFormatter generatesDecimalNumbers];
    NSLog(@"%d", status);
		

Output

2014-04-16 05:50:09.086 iOS-Tutorial[1361:a0b] 0
		

Converting Between Numbers and Strings

- numberFromString:

Returns an NSNumber object created by parsing a given string.

Example

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehaviorDefault];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSLog(@"%@", [numberFormatter numberFromString:@"23.45"]);
		

Output

2014-04-16 05:51:08.823 iOS-Tutorial[1374:a0b] 23.45
		

- stringFromNumber:

Returns a string containing the formatted value of the provided number object.

Example

NSNumber *number = [NSNumber numberWithFloat:23.034];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehaviorDefault];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSLog(@"%@", [numberFormatter stringFromNumber:number]);
		

Output

2014-04-16 05:51:51.581 iOS-Tutorial[1386:a0b] 23.034
		

+ localizedStringFromNumber:numberStyle:

Returns a localized number string with the specified style.

Example

NSNumber *number = [NSNumber numberWithFloat:23.034];
NSLog(@"%@", [NSNumberFormatter localizedStringFromNumber:number numberStyle:NSNumberFormatterDecimalStyle]);
		

Output

2014-04-16 05:52:56.665 iOS-Tutorial[1398:a0b] 23.034
		

Managing Localization of Numbers

- setLocale:

Sets the locale of the receiver.

Example

NSNumber *number = [NSNumber numberWithFloat:23.034];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehaviorDefault];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"fr_US"]];
NSLog(@"%@", [numberFormatter stringFromNumber:number]);
		

Output

2014-04-16 05:55:08.985 iOS-Tutorial[1415:a0b] 23,034
		

- locale

Returns the locale of the receiver.

Example

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehaviorDefault];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"fr_US"]];
NSLog(@"%@", [numberFormatter locale]);
		

Output

2014-04-16 05:55:53.553 iOS-Tutorial[1428:a0b] <__NSCFLocale: 0xa05c150>
		

Configuring Rounding Behavior

- setRoundingIncrement:

Sets the rounding increment used by the receiver.

Example

NSNumber *number = [NSNumber numberWithFloat:23.034];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehaviorDefault];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"fr_US"]];
[numberFormatter setRoundingIncrement:[NSNumber numberWithFloat:0.0]];
NSLog(@"%@", [numberFormatter stringFromNumber:number]);
		

Output

2014-04-16 05:59:28.461 iOS-Tutorial[1539:a0b] 23,034
		

- roundingIncrement

Returns the rounding increment used by the receiver.

Example

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehaviorDefault];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"fr_US"]];
[numberFormatter setRoundingIncrement:[NSNumber numberWithFloat:1.0]];
NSLog(@"%@", [numberFormatter roundingIncrement]);
		

Output

2014-04-16 06:00:17.201 iOS-Tutorial[1563:a0b] 1
		

- setRoundingMode:

Sets the rounding mode used by the receiver.

Example

NSNumber *number = [NSNumber numberWithFloat:23.034];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehaviorDefault];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"fr_US"]];
[numberFormatter setRoundingMode:NSNumberFormatterRoundCeiling];
NSLog(@"%@", [numberFormatter stringFromNumber:number]);
		

Output

2014-04-16 06:01:28.676 iOS-Tutorial[1573:a0b] 23,035
		

- roundingMode

Returns the rounding mode used by the receiver.

Example

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehaviorDefault];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"fr_US"]];
[numberFormatter setRoundingMode:NSNumberFormatterRoundUp];
NSLog(@"%d", [numberFormatter roundingMode]);
		

Output

2014-04-16 06:03:07.704 iOS-Tutorial[1599:a0b] 3
		

Configuring Numeric Formats

- setFormatWidth:

Sets the format width used by the receiver.

Example

NSNumber *number = [NSNumber numberWithFloat:23.034];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehaviorDefault];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"fr_US"]];
[numberFormatter setFormatWidth:8];
NSLog(@"%@", [numberFormatter stringFromNumber:number]);
		

Output

2014-04-16 06:05:07.078 iOS-Tutorial[1675:a0b] **23,034
		

- formatWidth

Returns the format width of the receiver.

Example

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehaviorDefault];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"fr_US"]];
[numberFormatter setFormatWidth:8];
NSLog(@"%d", [numberFormatter formatWidth]);		
	

Output

2014-04-16 06:05:53.798 iOS-Tutorial[1687:a0b] 8
		

- setNegativeFormat:

Sets the format the receiver uses to display negative values.

Example

NSNumber *number = [NSNumber numberWithFloat:-23.03];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehaviorDefault];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"fr_US"]];
[numberFormatter setCurrencySymbol:@"$"];
[numberFormatter setNegativeFormat:@"-¤0.00"];
[numberFormatter setPositiveFormat:@"¤0.00"];
NSLog(@"%@", [numberFormatter stringFromNumber:number]);
		

Output

2014-04-16 06:16:03.426 iOS-Tutorial[1942:a0b] -$23,03
		

- negativeFormat

Returns the format used by the receiver to display negative numbers.

Example

NSNumber *number = [NSNumber numberWithFloat:-23.03];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehaviorDefault];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"fr_US"]];
[numberFormatter setCurrencySymbol:@"$"];
[numberFormatter setNegativeFormat:@"-¤0.00"];
[numberFormatter setPositiveFormat:@"¤0.00"];
NSLog(@"%@", [numberFormatter negativeFormat]);		
	

Output

2014-04-16 06:20:54.350 iOS-Tutorial[1961:a0b] -¤0.00
		

- setPositiveFormat:

Sets the format the receiver uses to display positive values.

Example

NSNumber *number = [NSNumber numberWithFloat:23.03];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehaviorDefault];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"fr_US"]];
[numberFormatter setCurrencySymbol:@"$"];
[numberFormatter setNegativeFormat:@"-¤0.00"];
[numberFormatter setPositiveFormat:@"¤0.00"];
NSLog(@"%@", [numberFormatter stringFromNumber:number]);
		

Output

2014-04-16 06:16:03.426 iOS-Tutorial[1942:a0b] $23,03
		

- positiveFormat

Returns the format used by the receiver to display positive numbers.

Example

NSNumber *number = [NSNumber numberWithFloat:-23.03];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehaviorDefault];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"fr_US"]];
[numberFormatter setCurrencySymbol:@"$"];
[numberFormatter setNegativeFormat:@"-¤0.00"];
[numberFormatter setPositiveFormat:@"¤0.00"];
NSLog(@"%@", [numberFormatter positiveFormat]);	
		

Output

2014-04-16 06:20:54.350 iOS-Tutorial[1961:a0b] ¤0.00
		

- setMultiplier:

Sets the multiplier of the receiver.

Example

NSNumber *number = [NSNumber numberWithFloat:23.03];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehaviorDefault];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"fr_US"]];
[numberFormatter setCurrencySymbol:@"$"];
[numberFormatter setNegativeFormat:@"-¤0.00"];
[numberFormatter setPositiveFormat:@"¤0.00"];
[numberFormatter setMultiplier:[NSNumber numberWithFloat:1.5]];
NSLog(@"%@", [numberFormatter stringFromNumber:number]);
		

Output

2014-04-16 06:22:59.277 iOS-Tutorial[1977:a0b] $34,55
		

- multiplier

Returns the multiplier used by the receiver as an NSNumber object.

Example

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehaviorDefault];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"fr_US"]];
[numberFormatter setCurrencySymbol:@"$"];
[numberFormatter setNegativeFormat:@"-¤0.00"];
[numberFormatter setPositiveFormat:@"¤0.00"];
[numberFormatter setMultiplier:[NSNumber numberWithFloat:1.5]];
NSLog(@"%@", [numberFormatter multiplier]);
		

Output

2014-04-16 06:23:56.736 iOS-Tutorial[1992:a0b] 1.5
		

Advertisements