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)

Adds this number to another given number.

func subtracting(NSDecimalNumber)

Subtracts another given number from this one.

func multiplying(by: NSDecimalNumber)

Multiplies the number by another given number.

func dividing(by: NSDecimalNumber)

Divides the number by another given number.

func raising(toPower: Int)

Raises the number to a given power.

func multiplying(byPowerOf10: Int16)

Multiplies the number by 10 raised to the given power.

func adding(NSDecimalNumber, withBehavior: NSDecimalNumberBehaviors?)

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

func subtracting(NSDecimalNumber, withBehavior: NSDecimalNumberBehaviors?)

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

func multiplying(by: NSDecimalNumber, withBehavior: NSDecimalNumberBehaviors?)

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

func dividing(by: NSDecimalNumber, withBehavior: NSDecimalNumberBehaviors?)

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

func raising(toPower: Int, withBehavior: NSDecimalNumberBehaviors?)

Raises the number to a given power using the specified behavior.

func multiplying(byPowerOf10: Int16, withBehavior: NSDecimalNumberBehaviors?)

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

Rounding Off

func rounding(accordingToBehavior: NSDecimalNumberBehaviors?)

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?)

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

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)

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.