# Fractions

## Fraction <a href="#fraction" id="fraction"></a>

```
constructor(numerator: BigintIsh, denominator: BigintIsh = ONE)
```

The base class which all subsequent fraction classes extend. **Not meant to be used directly.**

### Properties <a href="#properties" id="properties"></a>

#### numerator <a href="#numerator" id="numerator"></a>

```
numerator: JSBI
```

#### denominator <a href="#denominator" id="denominator"></a>

```
denominator: JSBI
```

#### quotient <a href="#quotient" id="quotient"></a>

```
quotient: JSBI
```

Performs floor division.

### Methods <a href="#methods" id="methods"></a>

#### invert <a href="#invert" id="invert"></a>

```
invert(): Fraction
```

#### add <a href="#add" id="add"></a>

```
add(other: Fraction | BigintIsh): Fraction
```

#### subtract <a href="#subtract" id="subtract"></a>

```
subtract(other: Fraction | BigintIsh): Fraction
```

#### multiply <a href="#multiply" id="multiply"></a>

```
multiply(other: Fraction | BigintIsh): Fraction
```

#### divide <a href="#divide" id="divide"></a>

```
divide(other: Fraction | BigintIsh): Fraction
```

#### toSignificant <a href="#tosignificant" id="tosignificant"></a>

```
toSignificant(
  significantDigits: number,
  format: object = { groupSeparator: '' },
  rounding: Rounding = Rounding.ROUND_HALF_UP
): string
```

Formats a fraction to the specified number of significant digits.

* For format options, see [toFormat](https://github.com/MikeMcl/toFormat).

#### toFixed <a href="#tofixed" id="tofixed"></a>

```
toFixed(
  decimalPlaces: number,
  format: object = { groupSeparator: '' },
  rounding: Rounding = Rounding.ROUND_HALF_UP
): string
```

Formats a fraction to the specified number of decimal places.

* For format options, see [toFormat](https://github.com/MikeMcl/toFormat).

## Percent <a href="#percent" id="percent"></a>

Responsible for formatting percentages (10% instead of 0.1).

### Example <a href="#example" id="example"></a>

```
import { Percent } from '@whiteswap/sdk'

const percent = new Percent('60', '100')
console.log(percent.toSignificant(2)) // 60
```

#### toSignificant <a href="#tosignificant-1" id="tosignificant-1"></a>

See [toSignificant](/reference/sdk/fractions.md#tosignificant).

#### toFixed <a href="#tofixed-1" id="tofixed-1"></a>

See [toFixed](/reference/sdk/fractions.md#tofixed).

## TokenAmount <a href="#tokenamount" id="tokenamount"></a>

```
constructor(token: Token, amount: BigintIsh)
```

Responsible for formatting token amounts with specific decimal places.

### Example <a href="#example-1" id="example-1"></a>

```
import { Token, TokenAmount } from '@whiteswap/sdk'

const FRIED = new Token(ChainId.MAINNET, '0xfa1aFe1000000000000000000000000000000000', 18, 'FRIED', 'Beans')

const tokenAmount = new TokenAmount(FRIED, '3000000000000000000')
console.log(tokenAmount.toExact()) // 3
```

### Properties <a href="#properties-1" id="properties-1"></a>

#### token <a href="#token" id="token"></a>

```
token: Token
```

#### raw <a href="#raw" id="raw"></a>

```
raw: JSBI
```

Returns the full token amount, unadjusted for decimals.

### Methods <a href="#methods-1" id="methods-1"></a>

#### add <a href="#add-1" id="add-1"></a>

```
add(other: TokenAmount): TokenAmount
```

#### subtract <a href="#subtract-1" id="subtract-1"></a>

```
subtract(other: TokenAmount): TokenAmount
```

#### toSignificant <a href="#tosignificant-2" id="tosignificant-2"></a>

See [toSignificant](/reference/sdk/fractions.md#tosignificant-2).

#### toFixed <a href="#tofixed-2" id="tofixed-2"></a>

See [toFixed](/reference/sdk/fractions.md#tofixed-1).

#### toExact <a href="#toexact" id="toexact"></a>

```
toExact(format: object = { groupSeparator: '' }): string
```

## Price <a href="#price" id="price"></a>

```
constructor(baseToken: Token, quoteToken: Token, denominator: BigintIsh, numerator: BigintIsh)
```

Responsible for denominating the relative price between two tokens. Denominator and numerator must be unadjusted for decimals.

### Example <a href="#example-2" id="example-2"></a>

```
import { ChainId, WETH as WETHs, Token, Price } from '@whiteswap/sdk'

const WETH = WETHs[ChainId.MAINNET]
const ABC = new Token(ChainId.MAINNET, '0xabc0000000000000000000000000000000000000', 18, 'ABC')

const price = new Price(WETH, ABC, '1000000000000000000', '123000000000000000000')
console.log(price.toSignificant(3)) // 123
```

This example shows the ETH/XYZ price, where ETH is the base token, and XYZ is the quote token. The price is constructed from an amount of XYZ (the numerator) / an amount of WETH (the denominator).

### Static Methods <a href="#static-methods" id="static-methods"></a>

#### fromRoute <a href="#fromroute" id="fromroute"></a>

```
fromRoute(route: Route): Price
```

### Properties <a href="#properties-2" id="properties-2"></a>

#### baseToken <a href="#basetoken" id="basetoken"></a>

```
baseToken: Token
```

#### quoteToken <a href="#quotetoken" id="quotetoken"></a>

```
quoteToken: Token
```

#### scalar <a href="#scalar" id="scalar"></a>

```
scalar: Fraction
```

Used to adjust the price for the decimals of the base and quote tokens.

#### raw <a href="#raw-1" id="raw-1"></a>

```
raw: Fraction
```

Returns the raw price, unadjusted for decimals.

#### adjusted <a href="#adjusted" id="adjusted"></a>

```
adjusted: Fraction
```

Returns the price, adjusted for decimals.

### Methods <a href="#methods-2" id="methods-2"></a>

#### invert <a href="#invert-1" id="invert-1"></a>

```
invert(): Price
```

#### multiply <a href="#multiply-1" id="multiply-1"></a>

```
multiply(other: Price): Price
```

#### quote <a href="#quote" id="quote"></a>

```
quote(tokenAmount: TokenAmount): TokenAmount
```

Given an asset amount, returns an equivalent value of the other asset, according to the current price.

#### toSignificant <a href="#tosignificant-3" id="tosignificant-3"></a>

See [toSignificant](/reference/sdk/fractions.md#tosignificant).

#### toFixed <a href="#tofixed-3" id="tofixed-3"></a>

See [toFixed](/reference/sdk/fractions.md#tofixed).


---

# 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://docs.ws.exchange/reference/sdk/fractions.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.
