Skip to main content

Overview

Drift Protocol v2 uses BN.js (BigNum) to represent numerical values with high precision. This is essential because Solana tokens use levels of precision that exceed JavaScript’s standard floating-point number capabilities.
All numbers in BN are represented as integers. The precision value indicates how to convert them back to decimal numbers.

How Precision Works

A BigNum value combined with a precision represents a decimal number:

Standard Precision Constants

Drift uses consistent precision values across the protocol:

Precision Relationships

The BigNum Class

Drift provides a BigNum class that wraps BN with precision-aware operations:

Creating BigNum Values

Arithmetic Operations

Shifting Precision

Display & Formatting

Division Precision Handling

BN division returns the floor value by default. For exact division, you must handle the modulus.

convertToNumber Helper

The SDK provides a convertToNumber function for safe BN to number conversion:

Implementation

convertToBN Helper

Convert JavaScript numbers to BN with proper precision:

Implementation

Common Patterns

Working with Prices

Working with Position Sizes

Calculating Notional Value

Best Practices

  1. Always use BN for calculations - Never convert to numbers until the final display step
  2. Match precisions - Ensure values have the same precision before adding/subtracting
  3. Use helper functions - Prefer convertToNumber() over manual division
  4. Be careful with multiplication - Precision accumulates, so adjust accordingly
  5. Handle division carefully - Remember BN division truncates by default

Examples from SDK

Trading Example