Class: NSDecimalNumber

Inherits:
NSNumber show all

Overview

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.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from NSNumber

#boolValue, #charValue, #floatValue, #initWithBool:, #initWithChar:, #initWithDouble:, #initWithFloat:, #initWithInt:, #initWithInteger:, #initWithLong:, #initWithLongLong:, #initWithShort:, #initWithUnsignedChar:, #initWithUnsignedInt:, #initWithUnsignedInteger:, #initWithUnsignedLong:, #initWithUnsignedLongLong:, #initWithUnsignedShort:, #intValue, #integerValue, #isEqualToNumber:, #longLongValue, #longValue, numberWithBool:, numberWithChar:, numberWithDouble:, numberWithFloat:, numberWithInt:, numberWithInteger:, numberWithLong:, numberWithLongLong:, numberWithShort:, numberWithUnsignedChar:, numberWithUnsignedInt:, numberWithUnsignedInteger:, numberWithUnsignedLong:, numberWithUnsignedLongLong:, numberWithUnsignedShort:, #shortValue, #stringValue, #unsignedCharValue, #unsignedIntValue, #unsignedIntegerValue, #unsignedLongLongValue, #unsignedLongValue, #unsignedShortValue

Methods inherited from NSValue

#getValue:, #initWithBytes:objCType:, #isEqualToValue:, #nonretainedObjectValue, #pointerValue, #rangeValue, value:withObjCType:, valueWithBytes:objCType:, valueWithNonretainedObject:, valueWithPointer:, valueWithRange:

Methods inherited from NSObject

#!, #!=, #!~, #, #==, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, alloc, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #eql?, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, #init, initialize, #initialize_clone, #initialize_copy, #initialize_dup, #inspect, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, #to_s, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version

Constructor Details

This class inherits a constructor from NSObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class NSObject

Class Method Details

+ (NSDecimalNumber) decimalNumberWithDecimal(decimal)

Creates and returns an NSDecimalNumber object equivalent to a given NSDecimal structure. You can initialize decimal programmatically or generate it using the NSScanner method, scanDecimal:

Parameters:

  • decimal (NSDecimal)

    An NSDecimal structure that specifies the value for the new decimal number object.

Returns:

+ (NSDecimalNumber) decimalNumberWithMantissa(mantissa, exponent:exponent, isNegative:isNegative)

Creates and returns an NSDecimalNumber object equivalent to the number specified by the arguments. 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 as 12345x10^–3—mantissa is 12345; exponent is –3; and isNegative is YES, as illustrated by the following example.

Parameters:

  • mantissa (Integer)

    The mantissa for the new decimal number object.

  • exponent (Integer)

    The exponent for the new decimal number object.

  • isNegative (Boolean)

    A Boolean value that specifies whether the sign of the number is negative.

Returns:

+ (NSDecimalNumber) decimalNumberWithString(numericString)

Creates and returns an NSDecimalNumber object whose value is equivalent to that in a given numeric string. Whether the NSDecimalSeparator is 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.

Parameters:

  • numericString (String)

    A numeric string.Besides digits, numericString can include an initial “+” or “–”; a single “E” or “e”, to indicate the exponent of a number in scientific notation; and a single NSDecimalSeparator to divide the fractional from the integral part of the number.

Returns:

+ (NSDecimalNumber) decimalNumberWithString(numericString, locale:locale)

Creates and returns an NSDecimalNumber object whose value is equivalent to that in a given numeric string, interpreted using a given locale. The locale parameter determines whether the NSDecimalSeparator is 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”

Parameters:

  • numericString (String)

    A numeric string.Besides digits, numericString can include an initial “+” or “–”; a single “E” or “e”, to indicate the exponent of a number in scientific notation; and a single NSDecimalSeparator to divide the fractional from the integral part of the number.

  • locale (Hash)

    A dictionary that defines the locale (specifically the NSDecimalSeparator) to use to interpret the number in numericString.

Returns:

+ (Object) defaultBehavior

Returns the way arithmetic methods, like decimalNumberByAdding:, round off and handle error conditions. By default, the arithmetic methods use the NSRoundPlain behavior; 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 with setDefaultBehavior:.

Returns:

+ (NSDecimalNumber) maximumDecimalNumber

Returns the largest possible value of an NSDecimalNumber object.

Returns:

+ (NSDecimalNumber) minimumDecimalNumber

Returns the smallest possible value of an NSDecimalNumber object.

Returns:

+ (NSDecimalNumber) notANumber

Returns an NSDecimalNumber object that specifies no number. Any arithmetic method receiving notANumber as an argument returns notANumber.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 the NSDecimalNumberBehaviors protocol specification.

Returns:

+ (NSDecimalNumber) one

Returns an NSDecimalNumber object equivalent to the number 1.0.

Returns:

+ (Object) setDefaultBehavior(behavior)

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

behavior must conform to the NSDecimalNumberBehaviors protocol.

Returns:

+ (NSDecimalNumber) zero

Returns an NSDecimalNumber object equivalent to the number 0.0.

Returns:

Instance Method Details

- (NSComparisonResult) compare(decimalNumber)

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

Parameters:

  • decimalNumber (NSNumber)

    The number with which to compare the receiver.This value must not be nil. If this value is nil, the behavior is undefined and may change in future versions of OS X.

Returns:

  • (NSComparisonResult)

    NSOrderedAscending if the value of decimalNumber is greater than the receiver; NSOrderedSame if they’re equal; and NSOrderedDescending if the value of decimalNumber is less than the receiver.

- (NSDecimalNumber) decimalNumberByAdding(decimalNumber)

Returns a new NSDecimalNumber object whose value is the sum of the receiver and another given NSDecimalNumber object. This method uses the default behavior when handling calculation errors and rounding.

Parameters:

Returns:

- (NSDecimalNumber) decimalNumberByAdding(decimalNumber, withBehavior:behavior)

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

behavior specifies the handling of calculation errors and rounding.

Returns:

- (NSDecimalNumber) decimalNumberByDividingBy(decimalNumber)

Returns a new NSDecimalNumber object whose value is the value of the receiver divided by that of another given NSDecimalNumber object. This method uses the default behavior when handling calculation errors and rounding.

Parameters:

  • decimalNumber (NSDecimalNumber)

    The number by which to divide the receiver.

Returns:

- (NSDecimalNumber) decimalNumberByDividingBy(decimalNumber, withBehavior:behavior)

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

behavior specifies the handling of calculation errors and rounding.

Returns:

- (NSDecimalNumber) decimalNumberByMultiplyingBy(decimalNumber)

Returns a new NSDecimalNumber object whose value is the value of the receiver multiplied by that of another given NSDecimalNumber object. This method uses the default behavior when handling calculation errors and when rounding.

Parameters:

  • decimalNumber (NSDecimalNumber)

    The number by which to multiply the receiver.

Returns:

- (NSDecimalNumber) decimalNumberByMultiplyingBy(decimalNumber, withBehavior:behavior)

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

behavior specifies the handling of calculation errors and rounding.

Returns:

- (NSDecimalNumber) decimalNumberByMultiplyingByPowerOf10(power)

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

This method uses the default behavior when handling calculation errors and when rounding.

Returns:

- (NSDecimalNumber) decimalNumberByMultiplyingByPowerOf10(power, withBehavior:behavior)

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

behavior specifies the handling of calculation errors and rounding.

Returns:

- (NSDecimalNumber) decimalNumberByRaisingToPower(power)

Returns a new NSDecimalNumber object whose value is the value of the receiver raised to a given power. This method uses the default behavior when handling calculation errors and when rounding.

Parameters:

  • power (Integer)

    The power to which to raise the receiver.

Returns:

- (NSDecimalNumber) decimalNumberByRaisingToPower(power, withBehavior:behavior)

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

behavior specifies the handling of calculation errors and rounding.

Returns:

- (NSDecimalNumber) decimalNumberByRoundingAccordingToBehavior(behavior)

Rounds the receiver off in the way specified by behavior and returns the result, a newly created NSDecimalNumber object. For a description of the different ways of rounding, see the roundingMode method in the NSDecimalNumberBehaviors protocol specification.

Returns:

- (NSDecimalNumber) decimalNumberBySubtracting(decimalNumber)

Returns a new NSDecimalNumber object whose value is that of another given NSDecimalNumber object subtracted from the value of the receiver. This method uses the default behavior when handling calculation errors and when rounding.

Parameters:

  • decimalNumber (NSDecimalNumber)

    The number to subtract from the receiver.

Returns:

- (NSDecimalNumber) decimalNumberBySubtracting(decimalNumber, withBehavior:behavior)

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

behavior specifies the handling of calculation errors and rounding.

Returns:

- (NSDecimal) decimalValue

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

Returns:

- (String) descriptionWithLocale(locale)

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

Parameters:

  • locale (Hash)

    A dictionary that defines the locale (specifically the NSDecimalSeparator) to use to generate the returned string.

Returns:

  • (String)

    A string that represents the contents of the receiver, according to locale.

- (Float) doubleValue

Returns the approximate value of the receiver as a double.

Returns:

  • (Float)

    The approximate value of the receiver as a double.

- (Object) initWithDecimal(decimal)

Returns an NSDecimalNumber object initialized to represent a given decimal. This method is the designated initializer for NSDecimalNumber.

Parameters:

  • decimal (NSDecimal)

    The value of the new object.

Returns:

- (Object) initWithMantissa(mantissa, exponent:exponent, isNegative:flag)

Returns an NSDecimalNumber object initialized using the given mantissa, exponent, and sign. 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—mantissa is 123; exponent is –2; and isNegative, which refers to the sign of the mantissa, is NO.

Parameters:

  • mantissa (Integer)

    The mantissa for the new decimal number object.

  • exponent (Integer)

    The exponent for the new decimal number object.

  • flag (Boolean)

    A Boolean value that specifies whether the sign of the number is negative.

Returns:

- (Object) initWithString(numericString)

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

Parameters:

  • numericString (String)

    A numeric string.Besides digits, numericString can include an initial “+” or “–”; a single “E” or “e”, to indicate the exponent of a number in scientific notation; and a single NSDecimalSeparator to divide the fractional from the integral part of the number. For a listing of acceptable and unacceptable strings, see the class method decimalNumberWithString:locale:.

Returns:

  • (Object)

    An NSDecimalNumber object initialized so that its value is equivalent to that in numericString.

- (Object) initWithString(numericString, locale:locale)

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

Parameters:

  • numericString (String)

    A numeric string.Besides digits, numericString can include an initial “+” or “–”; a single “E” or “e”, to indicate the exponent of a number in scientific notation; and a single NSDecimalSeparator to divide the fractional from the integral part of the number.

  • locale (Hash)

    A dictionary that defines the locale (specifically the NSDecimalSeparator) to use to interpret the number in numericString.

Returns:

  • (Object)

    An NSDecimalNumber object initialized so that its value is equivalent to that in numericString, interpreted using locale.

- (const char) 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).

Returns:

  • (const char)