Import Statement

Availability

Returns the number of digits allowed after the decimal separator. (required)

Declaration

Swift

funcscale() -> Int16

Objective-C

- (short)scale

Return Value

The number of digits allowed after the decimal separator.

Discussion

This method limits the precision of the values returned by NSDecimalNumber’s decimalNumberBy... methods. If scale returns a negative value, it affects the digits before the decimal separator as well. If scale returns NSDecimalNoScale, the number of digits is unlimited.

Assuming that roundingMode returns NSRoundPlain, different values of scale have the following effects on the number 123.456:

Parameters

method

The method that was being executed when the error occurred.

error

The type of error that was generated.

leftOperand

The left operand.

rightOperand

The right operand.

Discussion

There are four possible values for error, described in NSCalculationError. The first three have to do with limits on the ability of NSDecimalNumber to represent decimal numbers. An NSDecimalNumber object 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 between –256 and 256. The fourth results from the caller trying to divide by 0.

In implementing exceptionDuringOperation:error:leftOperand:rightOperand:, you can handle each of these errors in several ways:

Return nil. The calling method will return its value as though no error had occurred. If error is NSCalculationLossOfPrecision, method will return an imprecise value—that is, one constrained to 38 significant digits. If error is NSCalculationUnderflow or NSCalculationOverflow, method will return NSDecimalNumber's notANumber. You shouldn’t return nil if error is NSDivideByZero.

Correct the error and return a valid NSDecimalNumber object. The calling method will use this as its own return value.

Constants

Round to the closest possible return value; when caught halfway between two positive numbers, round up; when caught between two negative numbers, round down.

Available in iOS 2.0 and later.

RoundDown

NSRoundDown

Round return values down.

Available in iOS 2.0 and later.

RoundUp

NSRoundUp

Round return values up.

Available in iOS 2.0 and later.

RoundBankers

NSRoundBankers

Round to the closest possible return value; when halfway between two possibilities, return the possibility whose last digit is even.

In practice, this means that, over the long run, numbers will be rounded up as often as they are rounded down; there will be no systematic bias.

Available in iOS 2.0 and later.

Discussion

The rounding mode matters only if the scale method sets a limit on the precision of NSDecimalNumber return values. It has no effect if scale returns NSDecimalNoScale. Assuming that scale returns 1, the rounding mode has the following effects on various original values: