- Inherits from:
- NSObject
- Conforms to:
- NSDecimalNumberBehaviors
- NSObject (NSObject)
Declared in:
- Foundation/NSDecimalNumber.h
NSDecimalNumberHandler is a class that adopts the NSDecimalNumberBehaviors protocol. This class allows you to set the way an NSDecimalNumber rounds off and handles errors, without having to create a custom class.
You can use an instance of this class as an argument to any of the NSDecimalNumber methods that end with the word ...Behavior:. If you don't think you need special behavior, you probably don't need this class. The odds are that NSDecimalNumber's default behavior will suit your needs.
For more information, see the NSDecimalNumberBehaviors protocol specification.
NSDecimalNumberBehaviors
- - roundingMode
- - scale
- - exceptionDuringOperation:error:leftOperand:rightOperand:
- Creating and initializing an NSDecimalNumberHandler
- + defaultDecimalNumberHandler
- + decimalNumberHandlerWithRoundingMode:scale:raiseOnExactness:raiseOnOverflow:raiseOnUnderflow:raiseOnDivideByZero:
- Initializing an already-created NSDecimalNumberHandler
- - initWithRoundingMode:scale:raiseOnExactness:raiseOnOverflow:raiseOnUnderflow:raiseOnDivideByZero:
+ (id)defaultDecimalNumberHandler
This instance rounds to the closest possible return value. It assumes your need for precision does not exceed 38 significant digits, and it raises an exception when its NSDecimalNumber tries to divide by zero, or when its NSDecimalNumber produces a number is too big or too small to be represented.
+ (id)decimalNumberHandlerWithRoundingMode:(NSRoundingMode)roundingMode
scale:(short)scale
raiseOnExactness:(BOOL)raiseOnExactness
raiseOnOverflow:(BOOL)raiseOnOverflow
raiseOnUnderflow:(BOOL)raiseOnUnderflow
raiseOnDivideByZero:(BOOL)raiseOnDivideByZero
scale and roundingMode affect
the way the NSDecimalNumberHandler's NSDecimalNumber rounds off
its return value. scale sets the
number of digits a rounded value should have after its decimal point. roundingMode sets
the rule by which NSDecimalNumbers are rounded off. roundingMode has
four possiblevalues: NSRoundUp
, NSRoundDown
, NSRoundPlain
,
and NSRoundBankers
.
The raiseOn... arguments determine whether the NSDecimalNumberHandler will raise an exception when its NSDecimalNumber notifies it of a certain kind of calculation error. If a raiseOn... argument is YES, NSDecimalNumberHandler will raise an exception; if a raiseOn... argument is NO, NSDecimalNumber will ignore the error, and return control to the calling method.
See the NSDecimalNumberBehaviors protocol specification for a complete explanation of these possible behaviors.
- (id)initWithRoundingMode:(NSRoundingMode)roundingMode
scale:(short)scale
raiseOnExactness:(BOOL)raiseOnExactness
raiseOnOverflow:(BOOL)raiseOnOverflow
raiseOnUnderflow:(BOOL)raiseOnUnderflow
raiseOnDivideByZero:(BOOL)raiseOnDivideByZero
Returns self.