<!--
{
  "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/NumberFormatter/number(from:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSNumberFormatter(im)numberFromString:"
  },
  "title" : "number(from:)"
}
-->

# number(from:)

Returns an [`NSNumber`](/documentation/Foundation/NSNumber) object created by parsing a given string.

```
func number(from string: String) -> NSNumber?
```

## Parameters

`string`

An [`NSString`](/documentation/Foundation/NSString) object that is parsed to generate the returned number object.

## Return Value

An [`NSNumber`](/documentation/Foundation/NSNumber) object created by parsing `string` using the receiver’s format, or `nil` if no single number could be parsed.

## Discussion

If a string contains any characters other than numerical digits or locale-appropriate group or decimal separators, parsing will fail.

Any leading or trailing space separator characters in a string are ignored. For example, the strings “ 5”, “5 “, and “5” all produce the number `5`.

---

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)