<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSNumber",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSNumber"
  },
  "title" : "NSNumber"
}
-->

# NSNumber

An object wrapper for primitive scalar numeric values.

```
class NSNumber
```

## Overview

`NSNumber` is a subclass of `NSValue` that offers a value as any C scalar (numeric) type. It defines a set of methods specifically for setting and accessing the value as a signed or unsigned `char`, `short int`, `int`, `long int`, `long long int`, `float`, or `double` or as a `BOOL`. (Note that number objects do not necessarily preserve the type they are created with.) It also defines a [`compare(_:)`](/documentation/Foundation/NSNumber/compare(_:)) method to determine the ordering of two `NSNumber` objects.

`NSNumber` is “toll-free bridged” with its Core Foundation counterparts: <doc://com.apple.documentation/documentation/CoreFoundation/CFNumber> for integer and floating point values, and <doc://com.apple.documentation/documentation/CoreFoundation/CFBoolean> for Boolean values. See [Toll-Free Bridging](https://developer.apple.com/library/archive/documentation/General/Conceptual/CocoaEncyclopedia/Toll-FreeBridgin/Toll-FreeBridgin.html#//apple_ref/doc/uid/TP40010810-CH2) for more information on toll-free bridging.

### Value Conversions

`NSNumber` provides readonly properties that return the object’s stored value converted to a particular Boolean, integer, unsigned integer, or floating point C scalar type. Because numeric types have different storage capabilities, attempting to initialize with a value of one type and access the value of another type may produce an erroneous result—for example, initializing with a `double` value exceeding `FLT_MAX` and accessing its [`floatValue`](/documentation/Foundation/NSNumber/floatValue), or initializing with an negative integer value and accessing its [`uintValue`](/documentation/Foundation/NSNumber/uintValue). In some cases, attempting to initialize with a value of a type and access the value of another type may result in loss of precision—for example, initializing with a `double` value with many significant digits and accessing its [`floatValue`](/documentation/Foundation/NSNumber/floatValue), or initializing with a large integer value and accessing its [`int8Value`](/documentation/Foundation/NSNumber/int8Value).

An `NSNumber` object initialized with a value of a particular type accessing the converted value of a different *kind* of type, such as `unsigned int` and `float`, will convert its stored value to that converted type in the following ways:

|`Value`                                                  |``doc://com.apple.foundation/documentation/Foundation/NSNumber/boolValue``|``doc://com.apple.foundation/documentation/Foundation/NSNumber/intValue-95zzp``|``doc://com.apple.foundation/documentation/Foundation/NSNumber/uintValue``|``doc://com.apple.foundation/documentation/Foundation/NSNumber/floatValue``|
|---------------------------------------------------------|--------------------------------------------------------------------------|-------------------------------------------------------------------------------|--------------------------------------------------------------------------|---------------------------------------------------------------------------|
|<doc://com.apple.documentation/documentation/Swift/false>|<doc://com.apple.documentation/documentation/Swift/false>                 |`0`                                                                            |`0`                                                                       |`0.0`                                                                      |
|<doc://com.apple.documentation/documentation/Swift/true> |<doc://com.apple.documentation/documentation/Swift/true>                  |`1`                                                                            |`1`                                                                       |`1.0`                                                                      |

|`Value`|``doc://com.apple.foundation/documentation/Foundation/NSNumber/boolValue``|``doc://com.apple.foundation/documentation/Foundation/NSNumber/intValue-95zzp``|``doc://com.apple.foundation/documentation/Foundation/NSNumber/uintValue``|``doc://com.apple.foundation/documentation/Foundation/NSNumber/floatValue``|
|-------|--------------------------------------------------------------------------|-------------------------------------------------------------------------------|--------------------------------------------------------------------------|---------------------------------------------------------------------------|
|`0`    |<doc://com.apple.documentation/documentation/Swift/false>                 |`0`                                                                            |`0`                                                                       |`0.0`                                                                      |
|`1`    |<doc://com.apple.documentation/documentation/Swift/true>                  |`1`                                                                            |`1`                                                                       |`1.0`                                                                      |
|`-1`   |<doc://com.apple.documentation/documentation/Swift/true>                  |`-1`                                                                           |*invalid, erroneous result*                                               |`-1.0`                                                                     |

|`Value`|``doc://com.apple.foundation/documentation/Foundation/NSNumber/boolValue``|``doc://com.apple.foundation/documentation/Foundation/NSNumber/intValue-95zzp``|``doc://com.apple.foundation/documentation/Foundation/NSNumber/uintValue``|``doc://com.apple.foundation/documentation/Foundation/NSNumber/floatValue``|
|-------|--------------------------------------------------------------------------|-------------------------------------------------------------------------------|--------------------------------------------------------------------------|---------------------------------------------------------------------------|
|`0`    |<doc://com.apple.documentation/documentation/Swift/false>                 |`0`                                                                            |`0`                                                                       |`0.0`                                                                      |
|`1`    |<doc://com.apple.documentation/documentation/Swift/true>                  |`1`                                                                            |`1`                                                                       |`1.0`                                                                      |

|`Value`|``doc://com.apple.foundation/documentation/Foundation/NSNumber/boolValue``|``doc://com.apple.foundation/documentation/Foundation/NSNumber/intValue-95zzp``|``doc://com.apple.foundation/documentation/Foundation/NSNumber/uintValue``|``doc://com.apple.foundation/documentation/Foundation/NSNumber/floatValue``|
|-------|--------------------------------------------------------------------------|-------------------------------------------------------------------------------|--------------------------------------------------------------------------|---------------------------------------------------------------------------|
|`0.0`  |<doc://com.apple.documentation/documentation/Swift/false>                 |`0`                                                                            |`0`                                                                       |`0.0`                                                                      |
|`1.0`  |<doc://com.apple.documentation/documentation/Swift/true>                  |`1`                                                                            |`1`                                                                       |`1.0`                                                                      |
|`-1.0` |<doc://com.apple.documentation/documentation/Swift/true>                  |`-1`                                                                           |*invalid, erroneous result*                                               |`-1.0`                                                                     |

### Subclassing Notes

As with any class cluster, subclasses of `NSNumber` must override the primitive methods of its superclass, `NSValue`. In addition, there are two requirements around the data type your subclass represents:

1. Your implementation of [`objCType`](/documentation/Foundation/NSValue/objCType) must return one of “`c`”, “`C`”, “`s`”, “`S`”, “`i`”, “`I`”, “`l`”, “`L`”, “`q`”, “`Q`”, “`f`”, and “`d`”. This is required for the other methods of [`NSNumber`](/documentation/Foundation/NSNumber) to behave correctly.
2. Your subclass must override the accessor method that corresponds to the declared type—for example, if your implementation of [`objCType`](/documentation/Foundation/NSValue/objCType) returns  “`i`”, you must override [`int32Value`](/documentation/Foundation/NSNumber/int32Value).

## Topics

### Creating an NSNumber Object

[`+  numberWithBool:`](/documentation/Foundation/NSNumber/numberWithBool:)

Creates and returns an `NSNumber` object containing a given value, treating it as a `BOOL`.

[`+  numberWithChar:`](/documentation/Foundation/NSNumber/numberWithChar:)

Creates and returns an `NSNumber` object containing a given value, treating it as a signed `char`.

[`+  numberWithDouble:`](/documentation/Foundation/NSNumber/numberWithDouble:)

Creates and returns an `NSNumber` object containing a given value, treating it as a `double`.

[`+  numberWithFloat:`](/documentation/Foundation/NSNumber/numberWithFloat:)

Creates and returns an `NSNumber` object containing a given value, treating it as a `float`.

[`+  numberWithInt:`](/documentation/Foundation/NSNumber/numberWithInt:)

Creates and returns an `NSNumber` object containing a given value, treating it as a signed `int`.

[`+  numberWithInteger:`](/documentation/Foundation/NSNumber/numberWithInteger:)

Creates and returns an `NSNumber` object containing a given value, treating it as an `NSInteger`.

[`+  numberWithLong:`](/documentation/Foundation/NSNumber/numberWithLong:)

Creates and returns an `NSNumber` object containing a given value, treating it as a signed `long`.

[`+  numberWithLongLong:`](/documentation/Foundation/NSNumber/numberWithLongLong:)

Creates and returns an `NSNumber` object containing a given value, treating it as a signed `long long`.

[`+  numberWithShort:`](/documentation/Foundation/NSNumber/numberWithShort:)

Creates and returns an `NSNumber` object containing `value`, treating it as a signed `short`.

[`+  numberWithUnsignedChar:`](/documentation/Foundation/NSNumber/numberWithUnsignedChar:)

Creates and returns an `NSNumber` object containing a given value, treating it as an `unsigned char`.

[`+  numberWithUnsignedInt:`](/documentation/Foundation/NSNumber/numberWithUnsignedInt:)

Creates and returns an `NSNumber` object containing a given value, treating it as an `unsigned int`.

[`+  numberWithUnsignedInteger:`](/documentation/Foundation/NSNumber/numberWithUnsignedInteger:)

Creates and returns an `NSNumber` object containing a given value, treating it as an `NSUInteger`.

[`+  numberWithUnsignedLong:`](/documentation/Foundation/NSNumber/numberWithUnsignedLong:)

Creates and returns an `NSNumber` object containing a given value, treating it as an `unsigned long`.

[`+  numberWithUnsignedLongLong:`](/documentation/Foundation/NSNumber/numberWithUnsignedLongLong:)

Creates and returns an `NSNumber` object containing a given value, treating it as an `unsigned long long`.

[`+  numberWithUnsignedShort:`](/documentation/Foundation/NSNumber/numberWithUnsignedShort:)

Creates and returns an `NSNumber` object containing a given value, treating it as an `unsigned short`.

### Initializing an NSNumber Object

[`-  initWithBool:`](/documentation/Foundation/NSNumber/init(value:)-1ojz2)

Returns an `NSNumber` object initialized to contain a given value, treated as a `BOOL`.

[`-  initWithChar:`](/documentation/Foundation/NSNumber/init(value:)-8krjs)

Returns an `NSNumber` object initialized to contain a given value, treated as a signed `char`.

[`-  initWithDouble:`](/documentation/Foundation/NSNumber/init(value:)-15chk)

Returns an `NSNumber` object initialized to contain `value`, treated as a `double`.

[`-  initWithFloat:`](/documentation/Foundation/NSNumber/init(value:)-2vlwk)

Returns an `NSNumber` object initialized to contain a given value, treated as a `float`.

[`-  initWithInt:`](/documentation/Foundation/NSNumber/init(value:)-7jvmg)

Returns an `NSNumber` object initialized to contain a given value, treated as a signed `int`.

[`-  initWithInteger:`](/documentation/Foundation/NSNumber/init(value:)-5jcjl)

Returns an `NSNumber` object initialized to contain a given value, treated as an `NSInteger`.

[`-  initWithLong:`](/documentation/Foundation/NSNumber/initWithLong:)

Returns an `NSNumber` object initialized to contain a given value, treated as a signed `long`.

[`-  initWithLongLong:`](/documentation/Foundation/NSNumber/init(value:)-40ad0)

Returns an `NSNumber` object initialized to contain `value`, treated as a signed `long long`.

[`-  initWithShort:`](/documentation/Foundation/NSNumber/init(value:)-16drx)

Returns an `NSNumber` object initialized to contain a given value, treated as a signed `short`.

[`-  initWithUnsignedChar:`](/documentation/Foundation/NSNumber/init(value:)-8se67)

Returns an `NSNumber` object initialized to contain a given value, treated as an `unsigned char`.

[`-  initWithUnsignedInt:`](/documentation/Foundation/NSNumber/init(value:)-47coa)

Returns an `NSNumber` object initialized to contain a given value, treated as an `unsigned int`.

[`-  initWithUnsignedInteger:`](/documentation/Foundation/NSNumber/init(value:)-3l4ek)

Returns an `NSNumber` object initialized to contain a given value, treated as an `NSUInteger`.

[`-  initWithUnsignedLong:`](/documentation/Foundation/NSNumber/initWithUnsignedLong:)

Returns an `NSNumber` object initialized to contain a given value, treated as an `unsigned long`.

[`-  initWithUnsignedLongLong:`](/documentation/Foundation/NSNumber/init(value:)-43lc7)

Returns an `NSNumber` object initialized to contain a given value, treated as an `unsigned long long`.

[`-  initWithUnsignedShort:`](/documentation/Foundation/NSNumber/init(value:)-87y9m)

Returns an `NSNumber` object initialized to contain a given value, treated as an `unsigned short`.

### Accessing Numeric Values

[`boolValue`](/documentation/Foundation/NSNumber/boolValue)

The number object’s value expressed as a Boolean value.

[`charValue`](/documentation/Foundation/NSNumber/int8Value)

The number object’s value expressed as a `char`.

[`decimalValue`](/documentation/Foundation/NSNumber/decimalValue)

The number object’s value expressed as an [`Decimal`](/documentation/Foundation/Decimal) structure.

[`doubleValue`](/documentation/Foundation/NSNumber/doubleValue)

The number object’s value expressed as a `double`, converted as necessary.

[`floatValue`](/documentation/Foundation/NSNumber/floatValue)

The number object’s value expressed as a `float`, converted as necessary.

[`intValue`](/documentation/Foundation/NSNumber/int32Value)

The number object’s value expressed as an `int`, converted as necessary.

[`integerValue`](/documentation/Foundation/NSNumber/intValue-95zzp)

The number object’s value expressed as an `NSInteger` object, converted as necessary.

[`longLongValue`](/documentation/Foundation/NSNumber/int64Value)

The number object’s value expressed as a `long long`, converted as necessary.

[`longValue`](/documentation/Foundation/NSNumber/longValue)

The number object’s value expressed as a `long`, converted as necessary.

[`shortValue`](/documentation/Foundation/NSNumber/int16Value)

The number object’s value expressed as a `short`, converted as necessary.

[`unsignedCharValue`](/documentation/Foundation/NSNumber/uint8Value)

The number object’s value expressed as an unsigned `char`, converted as necessary.

[`unsignedIntegerValue`](/documentation/Foundation/NSNumber/uintValue)

The number object’s value expressed as an `NSUInteger` object, converted as necessary.

[`unsignedIntValue`](/documentation/Foundation/NSNumber/uint32Value)

The number object’s value expressed as an unsigned `int`, converted as necessary.

[`unsignedLongLongValue`](/documentation/Foundation/NSNumber/uint64Value)

The number object’s value expressed as an unsigned `long long`, converted as necessary.

[`unsignedLongValue`](/documentation/Foundation/NSNumber/unsignedLongValue)

The number object’s value expressed as an unsigned `long`, converted as necessary.

[`unsignedShortValue`](/documentation/Foundation/NSNumber/uint16Value)

The number object’s value expressed as an unsigned `short`, converted as necessary.

### Retrieving String Representations

[`-  descriptionWithLocale:`](/documentation/Foundation/NSNumber/description(withLocale:))

Returns a string that represents the contents of the number object for a given locale.

[`stringValue`](/documentation/Foundation/NSNumber/stringValue)

The number object’s value expressed as a human-readable string.

### Comparing NSNumber Objects

[`-  compare:`](/documentation/Foundation/NSNumber/compare(_:))

Returns an `NSComparisonResult` value that indicates whether the number object’s value is greater than, equal to, or less than a given number.

[`-  isEqualToNumber:`](/documentation/Foundation/NSNumber/isEqual(to:))

Returns a Boolean value that indicates whether the number object’s value and a given number are equal.

### Accessing Type Information

  <doc:nsnumber/1807278-objctype>

### Number Validation

[`NSDecimalIsNotANumber`](/documentation/Foundation/NSDecimalIsNotANumber(_:))

Returns a Boolean that indicates whether a given decimal contains a valid number.

## Relationships

### Conforms To

[`NSFetchRequestResult`](/documentation/CoreData/NSFetchRequestResult)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`CKRecordValueProtocol`](/documentation/CloudKit/CKRecordValueProtocol)

[`Escapable`](/documentation/Swift/Escapable)

[`CVarArg`](/documentation/Swift/CVarArg)

[`CKRecordValue-c.protocol`](/documentation/CloudKit/CKRecordValue-c.protocol)

[`NSSecureCoding`](/documentation/Foundation/NSSecureCoding)

[`NSCopying`](/documentation/Foundation/NSCopying)

[`ExpressibleByIntegerLiteral`](/documentation/Swift/ExpressibleByIntegerLiteral)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`Hashable`](/documentation/Swift/Hashable)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`Equatable`](/documentation/Swift/Equatable)

[`NSCoding`](/documentation/Foundation/NSCoding)

[`ExpressibleByFloatLiteral`](/documentation/Swift/ExpressibleByFloatLiteral)

[`Sendable`](/documentation/Swift/Sendable)

[`Copyable`](/documentation/Swift/Copyable)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`ExpressibleByBooleanLiteral`](/documentation/Swift/ExpressibleByBooleanLiteral)

### Inherits From

[`NSValue`](/documentation/Foundation/NSValue)

### Inherited By

[`NSDecimalNumber`](/documentation/Foundation/NSDecimalNumber)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)