# SafeCast

Contains methods for safely casting between types

### Functions[​](https://docs.uniswap.org/contracts/v3/reference/core/libraries/SafeCast#functions) <a href="#functions" id="functions"></a>

#### toUint160[​](https://docs.uniswap.org/contracts/v3/reference/core/libraries/SafeCast#touint160) <a href="#touint160" id="touint160"></a>

```solidity
  function toUint160(
    uint256 y
  ) internal pure returns (uint160 z)
```

Cast a uint256 to a uint160, revert on overflow

**Parameters:**[**​**](https://docs.uniswap.org/contracts/v3/reference/core/libraries/SafeCast#parameters)

| Name | Type    | Description                  |
| ---- | ------- | ---------------------------- |
| `y`  | uint256 | The uint256 to be downcasted |

**Return Values:**[**​**](https://docs.uniswap.org/contracts/v3/reference/core/libraries/SafeCast#return-values)

| Name | Type    | Description                              |
| ---- | ------- | ---------------------------------------- |
| `z`  | uint160 | The downcasted integer, now type uint160 |

#### toInt128[​](https://docs.uniswap.org/contracts/v3/reference/core/libraries/SafeCast#toint128) <a href="#toint128" id="toint128"></a>

```solidity
  function toInt128(
    int256 y
  ) internal pure returns (int128 z)
```

Cast a int256 to a int128, revert on overflow or underflow

**Parameters:**[**​**](https://docs.uniswap.org/contracts/v3/reference/core/libraries/SafeCast#parameters-1)

| Name | Type   | Description                 |
| ---- | ------ | --------------------------- |
| `y`  | int256 | The int256 to be downcasted |

**Return Values:**[**​**](https://docs.uniswap.org/contracts/v3/reference/core/libraries/SafeCast#return-values-1)

| Name | Type   | Description                             |
| ---- | ------ | --------------------------------------- |
| `z`  | int128 | The downcasted integer, now type int128 |

#### toInt256[​](https://docs.uniswap.org/contracts/v3/reference/core/libraries/SafeCast#toint256) <a href="#toint256" id="toint256"></a>

```solidity
  function toInt256(
    uint256 y
  ) internal pure returns (int256 z)
```

Cast a uint256 to a int256, revert on overflow

**Parameters:**[**​**](https://docs.uniswap.org/contracts/v3/reference/core/libraries/SafeCast#parameters-2)

| Name | Type    | Description              |
| ---- | ------- | ------------------------ |
| `y`  | uint256 | The uint256 to be casted |

**Return Values:**[**​**](https://docs.uniswap.org/contracts/v3/reference/core/libraries/SafeCast#return-values-2)

| Name | Type   | Description                         |
| ---- | ------ | ----------------------------------- |
| `z`  | int256 | The casted integer, now type int256 |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bonsaiswap.gitbook.io/bonsaiswap-docs/developers/technical-reference/core/libraries/safecast.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
