NSDecimalNumber
NSDecimalNumber, an immutable subclass of NSNumber, provides an object-oriented wrapper for doing base-10 arithmetic. An instance can represent any number that can be expressed as mantissa x 10^exponent where mantissa is a decimal integer up to 38 digits long, and exponent is an integer from –128 through 127.
-
Creates and returns an
NSDecimalNumberobject equivalent to a givenNSDecimalstructure.Declaration
Objective-C
+ (NSDecimalNumber *)decimalNumberWithDecimal:(NSDecimal)decimalParameters
decimalAn
NSDecimalstructure that specifies the value for the new decimal number object.Return Value
An
NSDecimalNumberobject equivalent todecimal.Discussion
You can initialize
decimalprogrammatically or generate it using theNSScannermethod,scanDecimal:Availability
Available in OS X v10.0 and later.
-
Creates and returns an
NSDecimalNumberobject equivalent to the number specified by the arguments.Declaration
Objective-C
+ (NSDecimalNumber *)decimalNumberWithMantissa:(unsigned long long)mantissaexponent:(short)exponentisNegative:(BOOL)isNegativeParameters
mantissaThe mantissa for the new decimal number object.
exponentThe exponent for the new decimal number object.
isNegativeA Boolean value that specifies whether the sign of the number is negative.
Discussion
The arguments express a number in a kind of scientific notation that requires the mantissa to be an integer. So, for example, if the number to be represented is
–12.345, it is expressed as12345x10^–3—mantissais12345;exponentis–3; andisNegativeisYEStrue, as illustrated by the following example.NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithMantissa:12345exponent:-3isNegative:YES];
Availability
Available in OS X v10.0 and later.
-
Creates and returns an
NSDecimalNumberobject whose value is equivalent to that in a given numeric string.Declaration
Objective-C
+ (NSDecimalNumber *)decimalNumberWithString:(NSString *)numericStringParameters
numericStringA numeric string.
Besides digits,
numericStringcan include an initial “+” or “–”; a single “E” or “e”, to indicate the exponent of a number in scientific notation; and a singleNSLocaleDecimalSeparatorto divide the fractional from the integral part of the number.Return Value
An
NSDecimalNumberobject whose value is equivalent tonumericString.Discussion
Whether the
NSLocaleDecimalSeparatoris a period (as is used, for example, in the United States) or a comma (as is used, for example, in France) depends on the default locale.Availability
Available in OS X v10.0 and later.
See Also
-
Creates and returns an
NSDecimalNumberobject whose value is equivalent to that in a given numeric string, interpreted using a given locale.Declaration
Objective-C
+ (NSDecimalNumber *)decimalNumberWithString:(NSString *)numericStringlocale:(id)localeParameters
numericStringA numeric string.
Besides digits,
numericStringcan include an initial “+” or “–”; a single “E” or “e”, to indicate the exponent of a number in scientific notation; and a singleNSLocaleDecimalSeparatorto divide the fractional from the integral part of the number.localeA dictionary that defines the locale (specifically the
NSLocaleDecimalSeparator) to use to interpret the number innumericString.Return Value
An
NSDecimalNumberobject whose value is equivalent tonumericString.Discussion
The
localeparameter determines whether theNSLocaleDecimalSeparatoris a period (as is used, for example, in the United States) or a comma (as is used, for example, in France).The following strings show examples of acceptable values for
numericString:“2500.6” (or “2500,6”, depending on locale)
“–2500.6” (or “–2500.6”)
“–2.5006e3” (or “–2,5006e3”)
“–2.5006E3” (or “–2,5006E3”)
The following strings are unacceptable:
“2,500.6”
“2500 3/5”
“2.5006x10e3”
“two thousand five hundred and six tenths”
Availability
Available in OS X v10.0 and later.
See Also
-
Returns an
NSDecimalNumberobject equivalent to the number 1.0.Declaration
Swift
class func one() -> NSDecimalNumberObjective-C
+ (NSDecimalNumber *)oneReturn Value
An
NSDecimalNumberobject equivalent to the number 1.0.Availability
Available in OS X v10.0 and later.
See Also
-
Returns an
NSDecimalNumberobject equivalent to the number 0.0.Declaration
Swift
class func zero() -> NSDecimalNumberObjective-C
+ (NSDecimalNumber *)zeroReturn Value
An
NSDecimalNumberobject equivalent to the number 0.0.Availability
Available in OS X v10.0 and later.
See Also
-
Returns an
NSDecimalNumberobject that specifies no number.Declaration
Swift
class func notANumber() -> NSDecimalNumberObjective-C
+ (NSDecimalNumber *)notANumberReturn Value
An
NSDecimalNumberobject that specifies no number.Discussion
Any arithmetic method receiving
notANumberas an argument returnsnotANumber.This value can be a useful way of handling non-numeric data in an input file. This method can also be a useful response to calculation errors. For more information on calculation errors, see the
exceptionDuringOperation:error:leftOperand:rightOperand:method description in theNSDecimalNumberBehaviorsprotocol specification.Availability
Available in OS X v10.0 and later.
-
init(decimal:) - initWithDecimal:Designated InitializerReturns an
NSDecimalNumberobject initialized to represent a given decimal.Declaration
Swift
init(decimaldcm: NSDecimal)Objective-C
- (instancetype)initWithDecimal:(NSDecimal)decimalParameters
decimalThe value of the new object.
Return Value
An
NSDecimalNumberobject initialized to representdecimal.Discussion
This method is the designated initializer for
NSDecimalNumber.Availability
Available in OS X v10.0 and later.
-
Returns an
NSDecimalNumberobject initialized using the given mantissa, exponent, and sign.Declaration
Objective-C
- (instancetype)initWithMantissa:(unsigned long long)mantissaexponent:(short)exponentisNegative:(BOOL)flagParameters
mantissaThe mantissa for the new decimal number object.
exponentThe exponent for the new decimal number object.
flagA Boolean value that specifies whether the sign of the number is negative.
Return Value
An
NSDecimalNumberobject initialized using the given mantissa, exponent, and sign.Discussion
The arguments express a number in a type of scientific notation that requires the mantissa to be an integer. So, for example, if the number to be represented is 1.23, it is expressed as 123x10^–2—
mantissais 123;exponentis –2; andisNegative, which refers to the sign of the mantissa, isNOfalse.Availability
Available in OS X v10.0 and later.
-
Returns an
NSDecimalNumberobject initialized so that its value is equivalent to that in a given numeric string.Declaration
Swift
convenience init(stringnumberValue: String?)Objective-C
- (instancetype)initWithString:(NSString *)numericStringParameters
numericStringA numeric string.
Besides digits,
numericStringcan include an initial “+” or “–”; a single “E” or “e”, to indicate the exponent of a number in scientific notation; and a singleNSLocaleDecimalSeparatorto divide the fractional from the integral part of the number. For a listing of acceptable and unacceptable strings, see the class methoddecimalNumberWithString:locale:.Return Value
An
NSDecimalNumberobject initialized so that its value is equivalent to that innumericString.Availability
Available in OS X v10.0 and later.
-
Returns an
NSDecimalNumberobject initialized so that its value is equivalent to that in a given numeric string, interpreted using a given locale.Declaration
Objective-C
- (instancetype)initWithString:(NSString *)numericStringlocale:(id)localeParameters
numericStringA numeric string.
Besides digits,
numericStringcan include an initial “+” or “–”; a single “E” or “e”, to indicate the exponent of a number in scientific notation; and a singleNSLocaleDecimalSeparatorto divide the fractional from the integral part of the number.localeA dictionary that defines the locale (specifically the
NSLocaleDecimalSeparator) to use to interpret the number innumericString.Return Value
An
NSDecimalNumberobject initialized so that its value is equivalent to that innumericString, interpreted usinglocale.Availability
Available in OS X v10.0 and later.
See Also
-
Returns a new
NSDecimalNumberobject whose value is the sum of the receiver and another givenNSDecimalNumberobject.Declaration
Swift
func decimalNumberByAdding(_decimalNumber: NSDecimalNumber) -> NSDecimalNumberObjective-C
- (NSDecimalNumber *)decimalNumberByAdding:(NSDecimalNumber *)decimalNumberParameters
decimalNumberThe number to add to the receiver.
Return Value
A new
NSDecimalNumberobject whose value is the sum of the receiver anddecimalNumber.Discussion
This method uses the default behavior when handling calculation errors and rounding.
Availability
Available in OS X v10.0 and later.
-
Returns a new
NSDecimalNumberobject whose value is that of another givenNSDecimalNumberobject subtracted from the value of the receiver.Declaration
Swift
func decimalNumberBySubtracting(_decimalNumber: NSDecimalNumber) -> NSDecimalNumberObjective-C
- (NSDecimalNumber *)decimalNumberBySubtracting:(NSDecimalNumber *)decimalNumberParameters
decimalNumberThe number to subtract from the receiver.
Return Value
A new
NSDecimalNumberobject whose value isdecimalNumbersubtracted from the receiver.Discussion
This method uses the default behavior when handling calculation errors and when rounding.
Availability
Available in OS X v10.0 and later.
-
Returns a new
NSDecimalNumberobject whose value is the value of the receiver multiplied by that of another givenNSDecimalNumberobject.Declaration
Swift
func decimalNumberByMultiplyingBy(_decimalNumber: NSDecimalNumber) -> NSDecimalNumberObjective-C
- (NSDecimalNumber *)decimalNumberByMultiplyingBy:(NSDecimalNumber *)decimalNumberParameters
decimalNumberThe number by which to multiply the receiver.
Return Value
A new
NSDecimalNumberobject whose value isdecimalNumbermultiplied by the receiver.Discussion
This method uses the default behavior when handling calculation errors and when rounding.
Availability
Available in OS X v10.0 and later.
-
Returns a new
NSDecimalNumberobject whose value is the value of the receiver divided by that of another givenNSDecimalNumberobject.Declaration
Swift
func decimalNumberByDividingBy(_decimalNumber: NSDecimalNumber) -> NSDecimalNumberObjective-C
- (NSDecimalNumber *)decimalNumberByDividingBy:(NSDecimalNumber *)decimalNumberParameters
decimalNumberThe number by which to divide the receiver.
Return Value
A new
NSDecimalNumberobject whose value is the value of the receiver divided bydecimalNumber.Discussion
This method uses the default behavior when handling calculation errors and rounding.
Availability
Available in OS X v10.0 and later.
-
Returns a new
NSDecimalNumberobject whose value is the value of the receiver raised to a given power.Declaration
Swift
func decimalNumberByRaisingToPower(_power: Int) -> NSDecimalNumberObjective-C
- (NSDecimalNumber *)decimalNumberByRaisingToPower:(NSUInteger)powerParameters
powerThe power to which to raise the receiver.
Return Value
A new
NSDecimalNumberobject whose value is the value of the receiver raised to the powerpower.Discussion
This method uses the default behavior when handling calculation errors and when rounding.
Availability
Available in OS X v10.0 and later.
-
Multiplies the receiver by 10^
powerand returns the product, a newly createdNSDecimalNumberobject.Declaration
Swift
func decimalNumberByMultiplyingByPowerOf10(_power: Int16) -> NSDecimalNumberObjective-C
- (NSDecimalNumber *)decimalNumberByMultiplyingByPowerOf10:(short)powerDiscussion
This method uses the default behavior when handling calculation errors and when rounding.
Availability
Available in OS X v10.0 and later.
-
Adds
decimalNumberto the receiver and returns the sum, a newly createdNSDecimalNumberobject.Declaration
Swift
func decimalNumberByAdding(_decimalNumber: NSDecimalNumber, withBehaviorbehavior: NSDecimalNumberBehaviors?) -> NSDecimalNumberObjective-C
- (NSDecimalNumber *)decimalNumberByAdding:(NSDecimalNumber *)decimalNumberwithBehavior:(id<NSDecimalNumberBehaviors>)behaviorDiscussion
behaviorspecifies the handling of calculation errors and rounding.Availability
Available in OS X v10.0 and later.
-
Subtracts
decimalNumberfrom the receiver and returns the difference, a newly createdNSDecimalNumberobject.Declaration
Swift
func decimalNumberBySubtracting(_decimalNumber: NSDecimalNumber, withBehaviorbehavior: NSDecimalNumberBehaviors?) -> NSDecimalNumberObjective-C
- (NSDecimalNumber *)decimalNumberBySubtracting:(NSDecimalNumber *)decimalNumberwithBehavior:(id<NSDecimalNumberBehaviors>)behaviorDiscussion
behaviorspecifies the handling of calculation errors and rounding.Availability
Available in OS X v10.0 and later.
-
Multiplies the receiver by
decimalNumberand returns the product, a newly createdNSDecimalNumberobject.Declaration
Swift
func decimalNumberByMultiplyingBy(_decimalNumber: NSDecimalNumber, withBehaviorbehavior: NSDecimalNumberBehaviors?) -> NSDecimalNumberObjective-C
- (NSDecimalNumber *)decimalNumberByMultiplyingBy:(NSDecimalNumber *)decimalNumberwithBehavior:(id<NSDecimalNumberBehaviors>)behaviorDiscussion
behaviorspecifies the handling of calculation errors and rounding.Availability
Available in OS X v10.0 and later.
-
Divides the receiver by
decimalNumberand returns the quotient, a newly createdNSDecimalNumberobject.Declaration
Swift
func decimalNumberByDividingBy(_decimalNumber: NSDecimalNumber, withBehaviorbehavior: NSDecimalNumberBehaviors?) -> NSDecimalNumberObjective-C
- (NSDecimalNumber *)decimalNumberByDividingBy:(NSDecimalNumber *)decimalNumberwithBehavior:(id<NSDecimalNumberBehaviors>)behaviorDiscussion
behaviorspecifies the handling of calculation errors and rounding.Availability
Available in OS X v10.0 and later.
-
Raises the receiver to
powerand returns the result, a newly createdNSDecimalNumberobject.Declaration
Swift
func decimalNumberByRaisingToPower(_power: Int, withBehaviorbehavior: NSDecimalNumberBehaviors?) -> NSDecimalNumberObjective-C
- (NSDecimalNumber *)decimalNumberByRaisingToPower:(NSUInteger)powerwithBehavior:(id<NSDecimalNumberBehaviors>)behaviorDiscussion
behaviorspecifies the handling of calculation errors and rounding.Availability
Available in OS X v10.0 and later.
-
decimalNumberByMultiplyingByPowerOf10(_:withBehavior:) - decimalNumberByMultiplyingByPowerOf10:withBehavior:Multiplies the receiver by 10^
powerand returns the product, a newly createdNSDecimalNumberobject.Declaration
Swift
func decimalNumberByMultiplyingByPowerOf10(_power: Int16, withBehaviorbehavior: NSDecimalNumberBehaviors?) -> NSDecimalNumberObjective-C
- (NSDecimalNumber *)decimalNumberByMultiplyingByPowerOf10:(short)powerwithBehavior:(id<NSDecimalNumberBehaviors>)behaviorDiscussion
behaviorspecifies the handling of calculation errors and rounding.Availability
Available in OS X v10.0 and later.
-
Rounds the receiver off in the way specified by
behaviorand returns the result, a newly createdNSDecimalNumberobject.Declaration
Swift
func decimalNumberByRoundingAccordingToBehavior(_behavior: NSDecimalNumberBehaviors?) -> NSDecimalNumberObjective-C
- (NSDecimalNumber *)decimalNumberByRoundingAccordingToBehavior:(id<NSDecimalNumberBehaviors>)behaviorDiscussion
For a description of the different ways of rounding, see the
roundingModemethod in theNSDecimalNumberBehaviorsprotocol specification.Availability
Available in OS X v10.0 and later.
-
decimalValue decimalValueProperty -
doubleValue doubleValuePropertyThe decimal number’s closest approximate
doublevalue. (read-only)Declaration
Swift
var doubleValue: Double { get }Objective-C
@property(readonly) double doubleValueDiscussion
Not all decimal numbers can be accurately represented using a
doublevalue.Availability
Available in OS X v10.0 and later.
-
Returns a string, specified according to a given locale, that represents the contents of the receiver.
Declaration
Objective-C
- (NSString *)descriptionWithLocale:(id)localeParameters
localeA dictionary that defines the locale (specifically the
NSLocaleDecimalSeparator) to use to generate the returned string.Return Value
A string that represents the contents of the receiver, according to
locale.Availability
Available in OS X v10.0 and later.
-
A C string containing the Objective-C type for the data contained in the decimal number object. (read-only)
Declaration
Swift
var objCType: UnsafePointer<Int8> { get }Objective-C
@property(readonly) const char *objCTypeDiscussion
For a decimal number object, this property always contains “d” (for double).
Availability
Available in OS X v10.0 and later.
-
Returns the way arithmetic methods, like
decimalNumberByAdding:, round off and handle error conditions.Declaration
Swift
class func defaultBehavior() -> NSDecimalNumberBehaviorsObjective-C
+ (id<NSDecimalNumberBehaviors>)defaultBehaviorDiscussion
By default, the arithmetic methods use the
NSRoundPlainbehavior; that is, the methods round to the closest possible return value. The methods assume your need for precision does not exceed 38 significant digits and raise exceptions when they try to divide by 0 or produce a number too big or too small to be represented.If this default behavior doesn’t suit your application, you should use methods that let you specify the behavior, like
decimalNumberByAdding:withBehavior:. If you find yourself using a particular behavior consistently, you can specify a different default behavior withsetDefaultBehavior:.The default behavior is maintained separately for each thread in your app.
Availability
Available in OS X v10.0 and later.
-
Specifies the way that arithmetic methods, like
decimalNumberByAdding:, round off and handle error conditions.Declaration
Swift
class func setDefaultBehavior(_behavior: NSDecimalNumberBehaviors)Objective-C
+ (void)setDefaultBehavior:(id<NSDecimalNumberBehaviors>)behaviorDiscussion
behaviormust conform to theNSDecimalNumberBehaviorsprotocol.The default behavior is maintained separately for each thread in your app.
setDefaultBehavior:sets the default behavior for the thread on which it’s executed.Availability
Available in OS X v10.0 and later.
-
Returns an
NSComparisonResultvalue that indicates the numerical ordering of the receiver and another givenNSDecimalNumberobject.Declaration
Swift
func compare(_decimalNumber: NSNumber) -> NSComparisonResultObjective-C
- (NSComparisonResult)compare:(NSNumber *)decimalNumberParameters
decimalNumberThe number with which to compare the receiver.
This value must not be
nil. If this value isnil, the behavior is undefined and may change in future versions of OS X.Return Value
NSOrderedAscendingif the value ofdecimalNumberis greater than the receiver;NSOrderedSameif they’re equal; andNSOrderedDescendingif the value ofdecimalNumberis less than the receiver.Availability
Available in OS X v10.0 and later.
-
Returns the largest possible value of an
NSDecimalNumberobject.Declaration
Swift
class func maximumDecimalNumber() -> NSDecimalNumberObjective-C
+ (NSDecimalNumber *)maximumDecimalNumberReturn Value
The largest possible value of an
NSDecimalNumberobject.Availability
Available in OS X v10.0 and later.
See Also
-
Returns the smallest possible value of an
NSDecimalNumberobject.Declaration
Swift
class func minimumDecimalNumber() -> NSDecimalNumberObjective-C
+ (NSDecimalNumber *)minimumDecimalNumberReturn Value
The smallest possible value of an
NSDecimalNumberobject.Availability
Available in OS X v10.0 and later.
See Also
-
Names of the various exceptions raised by
NSDecimalNumberto indicate computational errors.Declaration
Swift
let NSDecimalNumberExactnessException: String let NSDecimalNumberOverflowException: String let NSDecimalNumberUnderflowException: String let NSDecimalNumberDivideByZeroException: StringObjective-C
extern NSString *NSDecimalNumberExactnessException; extern NSString *NSDecimalNumberOverflowException; extern NSString *NSDecimalNumberUnderflowException; extern NSString *NSDecimalNumberDivideByZeroException;Constants
-
NSDecimalNumberExactnessExceptionNSDecimalNumberExactnessExceptionThe name of the exception raised if there is an exactness error.
Available in OS X v10.0 and later.
-
NSDecimalNumberOverflowExceptionNSDecimalNumberOverflowExceptionThe name of the exception raised on overflow.
Available in OS X v10.0 and later.
-
NSDecimalNumberUnderflowExceptionNSDecimalNumberUnderflowExceptionThe name of the exception raised on underflow.
Available in OS X v10.0 and later.
-
NSDecimalNumberDivideByZeroExceptionNSDecimalNumberDivideByZeroExceptionThe name of the exception raised on divide by zero.
Available in OS X v10.0 and later.
-
Copyright © 2016 Apple Inc. All rights reserved. Terms of Use | Privacy Policy | Updated: 2013-09-17
