Creating a Decimal Number

+ decimalNumberWithDecimal:

Creates and returns an NSDecimalNumber object equivalent to a given NSDecimal structure.

Example

NSDecimalNumber *decimalNumber = [NSDecimalNumber decimalNumberWithDecimal:[[NSNumber numberWithDouble:10.95]decimalValue]];
NSLog(@"%f",[decimalNumber doubleValue]);
		

Output

2014-04-12 03:52:20.719 iOS-Tutorial[477:a0b] 10.950000
		

+ decimalNumberWithMantissa:exponent:isNegative:

Creates and returns an NSDecimalNumber object equivalent to the number specified by the arguments.

Example

NSDecimalNumber *decimalNumber = [NSDecimalNumber decimalNumberWithMantissa:3 exponent:2 isNegative:NO];
NSLog(@"%f",[decimalNumber doubleValue]);
		

Output

2014-04-12 03:55:26.599 iOS-Tutorial[586:a0b] 300.000000
		

+ decimalNumberWithString:

Creates and returns an NSDecimalNumber object whose value is equivalent to that in a given numeric string.

Example

NSDecimalNumber *decimalNumber = [NSDecimalNumber decimalNumberWithString:@"45.5656"];
NSLog(@"%f",[decimalNumber doubleValue]);
		

Output

2014-04-12 03:56:19.341 iOS-Tutorial[599:a0b] 45.565600
		

+ decimalNumberWithString:locale:

Creates and returns an NSDecimalNumber object whose value is equivalent to that in a given numeric string, interpreted using a given locale.

Example

NSDecimalNumber *decimalNumber = [NSDecimalNumber decimalNumberWithString:@"45.5656" locale:[NSLocale systemLocale]];
NSLog(@"%f",[decimalNumber doubleValue]);}
		

Output

2014-04-12 03:59:09.906 iOS-Tutorial[643:a0b] 45.565600
		

+ one

Returns an NSDecimalNumber object equivalent to the number 1.0.

Example

NSDecimalNumber *decimalNumber = [NSDecimalNumber one];
NSLog(@"%f",[decimalNumber doubleValue]);
		

Output

2014-04-12 04:08:26.077 iOS-Tutorial[763:a0b] 1.000000
		

+ zero

Returns an NSDecimalNumber object equivalent to the number 0.0.

Example

NSDecimalNumber *decimalNumber = [NSDecimalNumber zero];
NSLog(@"%f",[decimalNumber doubleValue]);
		

Output

2014-04-12 04:08:55.417 iOS-Tutorial[776:a0b] 0.000000
		

+ notANumber

Returns an NSDecimalNumber object that specifies no number.

Example

NSDecimalNumber *decimalNumber = [NSDecimalNumber notANumber];
NSLog(@"%f",[decimalNumber doubleValue]);
		

Output

2014-04-12 04:09:29.702 iOS-Tutorial[788:a0b] nan
		

Initializing a Decimal Number

- initWithDecimal:

Returns an NSDecimalNumber object initialized to represent a given decimal.

Example

NSDecimalNumber *decimalNumber = [[NSDecimalNumber alloc] initWithDecimal:[[NSNumber numberWithDouble:10.95]decimalValue]];
NSLog(@"%f",[decimalNumber doubleValue]);
		

Output

2014-04-12 04:11:05.276 iOS-Tutorial[800:a0b] 10.950000
		

- initWithMantissa:exponent:isNegative:

Returns an NSDecimalNumber object initialized using the given mantissa, exponent, and sign.

Example

NSDecimalNumber *decimalNumber = [[NSDecimalNumber alloc] initWithMantissa:3 exponent:2 isNegative:NO];
NSLog(@"%f",[decimalNumber doubleValue]);
		

Output

2014-04-12 04:12:07.680 iOS-Tutorial[813:a0b] 300.000000
		

- initWithString:

Returns an NSDecimalNumber object initialized so that its value is equivalent to that in a given numeric string.

Example

NSDecimalNumber *decimalNumber = [[NSDecimalNumber alloc] initWithString:@"10.5"];
NSLog(@"%f",[decimalNumber doubleValue]);
		

Output

2014-04-12 04:12:50.900 iOS-Tutorial[826:a0b] 10.500000
		

- initWithString:locale:

Returns an NSDecimalNumber object initialized so that its value is equivalent to that in a given numeric string, interpreted using a given locale.

Example

NSDecimalNumber *decimalNumber = [[NSDecimalNumber alloc] initWithString:@"10.5" locale:[NSLocale systemLocale]];
NSLog(@"%f",[decimalNumber doubleValue]);
		

Output

2014-04-12 04:13:28.442 iOS-Tutorial[839:a0b] 10.500000
		

Performing Arithmetic

- decimalNumberByAdding:

Returns a new NSDecimalNumber object whose value is the sum of the receiver and another given NSDecimalNumber object.

Example

NSDecimalNumber *decimalNumber1 = [[NSDecimalNumber alloc] initWithString:@"10.5"];
NSDecimalNumber *decimalNumber2 = [[NSDecimalNumber alloc] initWithString:@"23.3"];
decimalNumber1 = [decimalNumber1 decimalNumberByAdding:decimalNumber2];
NSLog(@"%f",[decimalNumber1 doubleValue]);
		

Output

2014-04-12 04:18:09.543 iOS-Tutorial[869:a0b] 33.800000
		

- decimalNumberBySubtracting:

Returns a new NSDecimalNumber object whose value is that of another given NSDecimalNumber object subtracted from the value of the receiver.

Example

NSDecimalNumber *decimalNumber1 = [[NSDecimalNumber alloc] initWithString:@"10.5"];
NSDecimalNumber *decimalNumber2 = [[NSDecimalNumber alloc] initWithString:@"23.3"];
decimalNumber1 = [decimalNumber1 decimalNumberBySubtracting:decimalNumber2];
NSLog(@"%f",[decimalNumber1 doubleValue]);
		

Output

2014-04-12 04:18:46.050 iOS-Tutorial[881:a0b] -12.800000
		

- decimalNumberByMultiplyingBy:

Returns a new NSDecimalNumber object whose value is the value of the receiver multiplied by that of another given NSDecimalNumber object.

Example

NSDecimalNumber *decimalNumber1 = [[NSDecimalNumber alloc] initWithString:@"10.5"];
NSDecimalNumber *decimalNumber2 = [[NSDecimalNumber alloc] initWithString:@"23.3"];
decimalNumber1 = [decimalNumber1 decimalNumberByMultiplyingBy:decimalNumber2];
NSLog(@"%f",[decimalNumber1 doubleValue]);
		

Output

2014-04-12 04:19:22.896 iOS-Tutorial[895:a0b] 244.650000
		

- decimalNumberByDividingBy:

Returns a new NSDecimalNumber object whose value is the value of the receiver divided by that of another given NSDecimalNumber object.

Example

NSDecimalNumber *decimalNumber1 = [[NSDecimalNumber alloc] initWithString:@"10.5"];
NSDecimalNumber *decimalNumber2 = [[NSDecimalNumber alloc] initWithString:@"23.3"];
decimalNumber1 = [decimalNumber1 decimalNumberByDividingBy:decimalNumber2];
NSLog(@"%f",[decimalNumber1 doubleValue]);
		

Output

2014-04-12 04:19:58.069 iOS-Tutorial[907:a0b] 0.450644
		

- decimalNumberByRaisingToPower:

Returns a new NSDecimalNumber object whose value is the value of the receiver raised to a given power.

Example

NSDecimalNumber *decimalNumber = [[NSDecimalNumber alloc] initWithString:@"10.5"];
decimalNumber = [decimalNumber decimalNumberByRaisingToPower:2];
NSLog(@"%f",[decimalNumber doubleValue]);
		

Output

2014-04-12 04:20:53.952 iOS-Tutorial[919:a0b] 110.250000
		

- decimalNumberByMultiplyingByPowerOf10:

Multiplies the receiver by 10^power and returns the product, a newly created NSDecimalNumber object.

Example

NSDecimalNumber *decimalNumber = [[NSDecimalNumber alloc] initWithString:@"10.5"];
decimalNumber = [decimalNumber decimalNumberByMultiplyingByPowerOf10:2];
NSLog(@"%f",[decimalNumber doubleValue]);
		

Output

2014-04-12 04:21:35.448 iOS-Tutorial[931:a0b] 1050.000000
		

- decimalNumberByAdding:withBehavior:

Adds decimalNumber to the receiver and returns the sum, a newly created NSDecimalNumber object.

Example

NSDecimalNumber *decimalNumber1 = [[NSDecimalNumber alloc] initWithString:@"10.5"];
NSDecimalNumber *decimalNumber2 = [[NSDecimalNumber alloc] initWithString:@"23.3"];
NSDecimalNumberHandler *roundUp = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:2 raiseOnExactness:YES raiseOnOverflow:YES raiseOnUnderflow:YES raiseOnDivideByZero:YES];
decimalNumber1 = [decimalNumber1 decimalNumberByAdding:decimalNumber2 withBehavior:roundUp];
NSLog(@"%f",[decimalNumber1 doubleValue]);
		

Output

2014-04-12 04:27:54.138 iOS-Tutorial[977:a0b] 33.800000
		

- decimalNumberBySubtracting:withBehavior:

Subtracts decimalNumber from the receiver and returns the difference, a newly created NSDecimalNumber object.

Example

NSDecimalNumber *decimalNumber1 = [[NSDecimalNumber alloc] initWithString:@"10.5"];
NSDecimalNumber *decimalNumber2 = [[NSDecimalNumber alloc] initWithString:@"23.3"];
NSDecimalNumberHandler *roundUp = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:2 raiseOnExactness:YES raiseOnOverflow:YES raiseOnUnderflow:YES raiseOnDivideByZero:YES];
decimalNumber1 = [decimalNumber1 decimalNumberBySubtracting:decimalNumber2 withBehavior:roundUp];
NSLog(@"%f",[decimalNumber1 doubleValue]);
		

Output

2014-04-12 04:28:24.164 iOS-Tutorial[989:a0b] -12.800000
		

- decimalNumberByMultiplyingBy:withBehavior:

Multiplies the receiver by decimalNumber and returns the product, a newly created NSDecimalNumber object.

Example

NSDecimalNumber *decimalNumber1 = [[NSDecimalNumber alloc] initWithString:@"10.5"];
NSDecimalNumber *decimalNumber2 = [[NSDecimalNumber alloc] initWithString:@"23.3"];
NSDecimalNumberHandler *roundUp = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:2 raiseOnExactness:YES raiseOnOverflow:YES raiseOnUnderflow:YES raiseOnDivideByZero:YES];
decimalNumber1 = [decimalNumber1 decimalNumberByMultiplyingBy:decimalNumber2 withBehavior:roundUp];
NSLog(@"%f",[decimalNumber1 doubleValue]);
		

Output

2014-04-12 04:29:07.677 iOS-Tutorial[1001:a0b] 244.650000
		

- decimalNumberByDividingBy:withBehavior:

Divides the receiver by decimalNumber and returns the quotient, a newly created NSDecimalNumber object.

Example

NSDecimalNumber *decimalNumber1 = [[NSDecimalNumber alloc] initWithString:@"10.5"];
NSDecimalNumber *decimalNumber2 = [[NSDecimalNumber alloc] initWithString:@"23.3"];
NSDecimalNumberHandler *roundUp = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:2 raiseOnExactness:YES raiseOnOverflow:YES raiseOnUnderflow:YES raiseOnDivideByZero:YES];
decimalNumber1 = [decimalNumber1 decimalNumberByDividingBy:decimalNumber2 withBehavior:roundUp];
NSLog(@"%f",[decimalNumber1 doubleValue]);
		

Output

2014-04-12 04:29:35.086 iOS-Tutorial[1013:a0b] 0.450000
		

- decimalNumberByRaisingToPower:withBehavior:

Raises the receiver to power and returns the result, a newly created NSDecimalNumber object.

Example

NSDecimalNumber *decimalNumber = [[NSDecimalNumber alloc] initWithString:@"10.5"];
NSDecimalNumberHandler *roundUp = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:2 raiseOnExactness:YES raiseOnOverflow:YES raiseOnUnderflow:YES raiseOnDivideByZero:YES];
decimalNumber = [decimalNumber decimalNumberByRaisingToPower:2 withBehavior:roundUp];
NSLog(@"%f",[decimalNumber doubleValue]);
		

Output

2014-04-12 04:30:31.748 iOS-Tutorial[1026:a0b] 110.250000
		

- decimalNumberByMultiplyingByPowerOf10:withBehavior:

Multiplies the receiver by 10^power and returns the product, a newly created NSDecimalNumber object.

Example

NSDecimalNumber *decimalNumber = [[NSDecimalNumber alloc] initWithString:@"10.5"];
NSDecimalNumberHandler *roundUp = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:2 raiseOnExactness:YES raiseOnOverflow:YES raiseOnUnderflow:YES raiseOnDivideByZero:YES];
decimalNumber = [decimalNumber decimalNumberByMultiplyingByPowerOf10:2 withBehavior:roundUp];
NSLog(@"%f",[decimalNumber doubleValue]);
		

Output

2014-04-12 04:31:03.996 iOS-Tutorial[1039:a0b] 1050.000000
		

Rounding Off

- decimalNumberByRoundingAccordingToBehavior:

Rounds the receiver off in the way specified by behavior and returns the result, a newly created NSDecimalNumber object.

Example

NSDecimalNumber *decimalNumber = [[NSDecimalNumber alloc] initWithString:@"10.5435345345"];
NSDecimalNumberHandler *roundUp = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:2 raiseOnExactness:YES raiseOnOverflow:YES raiseOnUnderflow:YES raiseOnDivideByZero:YES];
decimalNumber = [decimalNumber decimalNumberByRoundingAccordingToBehavior:roundUp];
NSLog(@"%f",[decimalNumber doubleValue]);
		

Output

2014-04-12 04:33:10.716 iOS-Tutorial[1072:a0b] 10.540000
		

Accessing the Value

- decimalValue

Returns the receiver’s value, expressed as an NSDecimal structure.

Example

NSDecimalNumber *decimalNumber = [[NSDecimalNumber alloc] initWithString:@"10.5435345345"];
NSLog(@"%d",[decimalNumber decimalValue]._exponent);
NSLog(@"%d",[decimalNumber decimalValue]._isCompact);
NSLog(@"%d",[decimalNumber decimalValue]._isNegative);
NSLog(@"%d",[decimalNumber decimalValue]._length);
NSLog(@"%d",[decimalNumber decimalValue]._mantissa[0]);
NSLog(@"%d",[decimalNumber decimalValue]._exponent);
		

Output

2014-04-12 04:36:40.497 iOS-Tutorial[1092:a0b] -10
2014-04-12 04:36:40.498 iOS-Tutorial[1092:a0b] 1
2014-04-12 04:36:40.498 iOS-Tutorial[1092:a0b] 0
2014-04-12 04:36:40.498 iOS-Tutorial[1092:a0b] 3
2014-04-12 04:36:40.499 iOS-Tutorial[1092:a0b] 45505
2014-04-12 04:36:40.499 iOS-Tutorial[1092:a0b] -10
		

- doubleValue

Returns the approximate value of the receiver as a double.

Example

NSDecimalNumber *decimalNumber = [[NSDecimalNumber alloc] initWithString:@"10.5435345345"];
NSLog(@"%f",[decimalNumber doubleValue]);
		

Output

2014-04-12 04:37:33.529 iOS-Tutorial[1106:a0b] 10.543535
		

- descriptionWithLocale:

Returns a string, specified according to a given locale, that represents the contents of the receiver.

Example

NSDecimalNumber *decimalNumber = [[NSDecimalNumber alloc] initWithString:@"10.5435345345"];
NSLog(@"%@",[decimalNumber descriptionWithLocale:[NSLocale systemLocale]])
		

Output

2014-04-12 04:38:14.355 iOS-Tutorial[1118:a0b] 10.5435345345
		

- objCType

Returns a C string containing the Objective-C type of the data contained in the receiver, which for an NSDecimalNumber object is always “d” (for double).

Example

NSDecimalNumber *decimalNumber = [[NSDecimalNumber alloc] initWithString:@"10.5435345345"];
NSLog(@"%s",[decimalNumber objCType])
		

Output

2014-04-12 04:38:49.278 iOS-Tutorial[1130:a0b] d
		

Managing Behavior

+ defaultBehavior

Returns the way arithmetic methods, like decimalNumberByAdding:, round off and handle error conditions.

Example

NSDecimalNumber *decimalNumber = [[NSDecimalNumber alloc] initWithString:@"10.5435345345"];
NSDecimalNumberHandler *behaviour = (NSDecimalNumberHandler *)[NSDecimalNumber defaultBehavior];
NSLog(@"%d",[behaviour scale]);
		

Output

2014-04-12 04:43:17.625 iOS-Tutorial[1184:a0b] 32767
		

+ setDefaultBehavior:

Specifies the way that arithmetic methods, like decimalNumberByAdding: , round off and handle error conditions.

Example

NSDecimalNumber *decimalNumber = [[NSDecimalNumber alloc] initWithString:@"10.5435345345"];
NSDecimalNumberHandler *behaviour = [[NSDecimalNumberHandler alloc]initWithRoundingMode:NSRoundPlain scale:3 raiseOnExactness:YES raiseOnOverflow:YES raiseOnUnderflow:YES raiseOnDivideByZero:YES];
[NSDecimalNumber setDefaultBehavior:behaviour];
NSLog(@"%f",[decimalNumber floatValue]);
		

Output

2014-04-12 04:45:39.108 iOS-Tutorial[1198:a0b] 10.543534
		

Comparing Decimal Numbers

- compare:

Returns an NSComparisonResult value that indicates the numerical ordering of the receiver and another given NSDecimalNumber object.

Example

NSDecimalNumber *decimalNumber1 = [[NSDecimalNumber alloc] initWithString:@"10.5"];
NSDecimalNumber *decimalNumber2 = [[NSDecimalNumber alloc] initWithString:@"23.3"];
NSComparisonResult result = [decimalNumber1 compare:decimalNumber2];
NSLog(@"%d",result);
		

Output

2014-04-12 04:48:17.554 iOS-Tutorial[1226:a0b] -1
		

Getting Maximum and Minimum Possible Values

+ maximumDecimalNumber

Returns the largest possible value of an NSDecimalNumber object.

Example

NSLog(@"%f",[[NSDecimalNumber maximumDecimalNumber] doubleValue]);
		

Output

2014-04-12 04:49:58.606 iOS-Tutorial[1251:a0b] 3402823669209386527134573422587986984989120495220592886182567909030848074563371292545093099671415093659185923505550104482059460129780644035015876086272801474166652928.000000
		

+ minimumDecimalNumber

Returns the smallest possible value of an NSDecimalNumber object.

Example

NSLog(@"%f",[[NSDecimalNumber minimumDecimalNumber] doubleValue]);
		

Output

2014-04-12 04:50:33.304 iOS-Tutorial[1263:a0b] -3402823669209386527134573422587986984989120495220592886182567909030848074563371292545093099671415093659185923505550104482059460129780644035015876086272801474166652928.000000
		

Advertisements