Class

# NSDecimalNumber

An object for representing and performing arithmetic on base-10 numbers that bridges to `Decimal`; use `NSDecimalNumber` when you need reference semantics or other Foundation-specific behavior.

## 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.

## Topics

### Creating a Decimal Number

`class var one: NSDecimalNumber`

A decimal number equivalent to the number 1.0.

`class var zero: NSDecimalNumber`

A decimal number equivalent to the number 0.0.

`class var notANumber: NSDecimalNumber`

A decimal number that specifies no number.

### Initializing a Decimal Number

`init(decimal: Decimal)`

Initializes a decimal number to represent a given decimal.

`init(mantissa: UInt64, exponent: Int16, isNegative: Bool)`

Initializes a decimal number using the given mantissa, exponent, and sign.

`init(string: String?)`

Initializes a decimal number so that its value is equivalent to that in a given numeric string.

`init(string: String?, locale: Any?)`

Initializes a decimal number so that its value is equivalent to that in a given numeric string, interpreted using a given locale.

### Performing Arithmetic

`func adding(NSDecimalNumber) -> NSDecimalNumber`

Adds this number to another given number.

`func subtracting(NSDecimalNumber) -> NSDecimalNumber`

Subtracts another given number from this one.

`func multiplying(byPowerOf10: Int16) -> NSDecimalNumber`

Multiplies the number by 10 raised to the given power.

`func adding(NSDecimalNumber, withBehavior: NSDecimalNumberBehaviors?) -> NSDecimalNumber`

Adds this number to another given number using the specified behavior.

`func subtracting(NSDecimalNumber, withBehavior: NSDecimalNumberBehaviors?) -> NSDecimalNumber`

Subtracts this a given number from this one using the specified behavior.

`func multiplying(by: NSDecimalNumber, withBehavior: NSDecimalNumberBehaviors?) -> NSDecimalNumber`

Multiplies this number by another given number using the specified behavior.

`func dividing(by: NSDecimalNumber, withBehavior: NSDecimalNumberBehaviors?) -> NSDecimalNumber`

Divides this number by another given number using the specified behavior.

`func multiplying(byPowerOf10: Int16, withBehavior: NSDecimalNumberBehaviors?) -> NSDecimalNumber`

Multiplies the number by 10 raised to the given power using the specified behavior.

### Rounding Off

`func rounding(accordingToBehavior: NSDecimalNumberBehaviors?) -> NSDecimalNumber`

Returns a rounded version of the decimal number using the specified rounding behavior.

### Managing Behavior

`class var defaultBehavior: NSDecimalNumberBehaviors`

The way arithmetic methods round off and handle error conditions.

`protocol NSDecimalNumberBehaviors`

A protocol that declares three methods that control the discretionary aspects of working with decimal numbers.

`class NSDecimalNumberHandler`

A class that adopts the decimal number behaviors protocol.

### Accessing the Value

`var decimalValue: Decimal`

The decimal number’s value, expressed as an `Decimal` structure.

`var doubleValue: Double`

The decimal number’s closest approximate `double` value.

`func description(withLocale: Any?) -> String`

Returns a string representation of the decimal number appropriate for the specified locale.

`var objCType: UnsafePointer<Int8>`

A C string containing the Objective-C type for the data contained in the decimal number object.

### Comparing Decimal Numbers

`func compare(NSNumber) -> ComparisonResult`

Compares this decimal number and another.

### Getting Maximum and Minimum Possible Values

`class var maximum: NSDecimalNumber`

Returns the largest possible value of a decimal number.

`class var minimum: NSDecimalNumber`

Returns the smallest possible value of a decimal number.

### Recognizing Exceptions

Exceptions with these names may be raised to indicate computational errors with decimal numbers.

## Relationships

### Inherits From

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.