---
title: "number(from:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/numberformatter/number(from:)"
---

# number(from:)

Returns an NSNumber object created by parsing a given string.

## Declaration

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

## Parameters

- `string`: An doc://com.apple.foundation/documentation/Foundation/NSString object that is parsed to generate the returned number object.

## Return Value

Return Value An NSNumber object created by parsing string using the receiver’s format, or nil if no single number could be parsed.

## Discussion

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.

## See Also

### Converting Between Numbers and Strings

- [getObjectValue(_:for:range:)](foundation/numberformatter/getobjectvalue(_:for:range:).md)
- [string(from:)](foundation/numberformatter/string(from:).md)
- [localizedString(from:number:)](foundation/numberformatter/localizedstring(from:number:).md)
