npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@solana/fixed-points

v8.0.0

Published

Fixed-point number types for JavaScript

Downloads

2,136,678

Readme

npm npm-downloads code-style-prettier

@solana/fixed-points

This package provides fixed-point number types for JavaScript without relying on floating-point arithmetic. It can be used standalone, but it is also exported as part of Kit @solana/kit.

This package is also part of the @solana/codecs package which acts as an entry point for all codec packages as well as for their documentation.

Two flavors are provided: decimal fixed-points (scale is a power of 10) and binary fixed-points (scale is a power of 2). Use decimal fixed-points to represent prices, token amounts, and any quantity whose precision aligns with decimal digits. Use binary fixed-points when the scale can align with bits — such as audio samples, graphics, or probabilities — and you want to trade base-10 ergonomics for faster arithmetic.

// Track a USDC balance without losing pennies to floating-point rounding.
const usdc = decimalFixedPoint('unsigned', 64, 6);
const balance = addDecimalFixedPoint(usdc('1234.56'), usdc('10.00'));
decimalFixedPointToString(balance); // "1244.56"

Both kinds share the same mental model. A value is a frozen object carrying a bigint raw and some shape metadata (signedness, total bits, scale). A decimal fixed-point represents raw / 10 ** decimals; a binary fixed-point represents raw / 2 ** fractionalBits.

Creating fixed-points

The decimalFixedPoint and binaryFixedPoint functions each validate a shape once and return a factory you can call many times to build values of that shape.

const usdc = decimalFixedPoint('unsigned', 64, 6); // u64 with 6 decimals
usdc('42.5'); // raw === 42_500_000n, exact
usdc('0.1234567'); // throws STRICT_MODE_PRECISION_LOSS (8th decimal is lossy)

const audioSample = binaryFixedPoint('signed', 16, 15); // Q1.15
audioSample('0.5'); // raw === 16384n, exact
audioSample('0.1', 'round'); // raw === 3277n, rounded

Use the raw* factories when the raw bigint is already known — for instance when you already have the value stored in its lowest denomination.

const cents = rawDecimalFixedPoint('unsigned', 32, 2);
cents(4250n); // represents 42.50

Use the ratio* factories for values defined as a numerator over a denominator.

const probability = ratioBinaryFixedPoint('signed', 16, 15);
probability(1n, 4n); // represents 0.25 exactly
probability(1n, 3n, 'round'); // represents ~0.333, rounded

Rounding modes

Any operation that could lose precision accepts a RoundingMode.

| Mode | Behavior | | -------- | ----------------------------------------------------------------- | | strict | Throws STRICT_MODE_PRECISION_LOSS when the result is not exact. | | floor | Rounds towards negative infinity. | | ceil | Rounds towards positive infinity. | | trunc | Rounds towards zero. | | round | Rounds half values away from zero. |

The default is 'strict', so precision loss is always explicit. Pass a mode whenever you are willing to accept a rounded result.

Arithmetic

Every arithmetic helper preserves the shape of its first operand and throws ARITHMETIC_OVERFLOW if the result doesn't fit.

addDecimalFixedPoint(usdc('10'), usdc('3.25')); // represents 13.25
subtractDecimalFixedPoint(usdc('10'), usdc('3.25')); // represents 6.75
negateBinaryFixedPoint(audioSample('0.5')); // represents -0.5 (signed only)
absoluteBinaryFixedPoint(audioSample('-0.5')); // represents 0.5

multiply and divide accept either another fixed-point of the same signedness (any total bits or scale) or a bare bigint. The result always has the first operand's shape. They consult the rounding mode when the operation is inexact; divide throws DIVISION_BY_ZERO on a zero denominator.

multiplyBinaryFixedPoint(audioSample('0.6'), audioSample('0.8')); // ~0.48
divideDecimalFixedPoint(usdc('100'), 4n); // represents 25
divideDecimalFixedPoint(usdc('1'), usdc('3'), 'floor'); // represents 0.333333

Comparisons

Comparison helpers return a boolean, or -1 | 0 | 1 for cmp. Only kind and scale must match; signedness and totalBits may differ.

cmpBinaryFixedPoint(audioSample('0.25'), audioSample('0.5')); // -1
eqBinaryFixedPoint(audioSample('0.5'), audioSample('0.5')); // true
ltDecimalFixedPoint(usdc('10'), usdc('20')); // true
lteDecimalFixedPoint(usdc('20'), usdc('20')); // true
gtDecimalFixedPoint(usdc('20'), usdc('10')); // true
gteDecimalFixedPoint(usdc('20'), usdc('20')); // true

Shape conversions

Use toSigned* and toUnsigned* to change a value's signedness without touching its total bits or scale.

const signedByte = rawBinaryFixedPoint('signed', 8, 4);
const unsignedByte = rawBinaryFixedPoint('unsigned', 8, 4);

toUnsignedBinaryFixedPoint(signedByte(100n)); // unsigned, raw === 100n
toSignedBinaryFixedPoint(unsignedByte(200n)); // throws: 200 > i8 max

Use rescale* to change the total bits and scale in one call. Scale-up is always exact; scale-down consults the rounding mode.

// Bridge EVM USDC (u128 with 18 decimals) down to SPL USDC (u64 with 6 decimals).
const evmUsdc = decimalFixedPoint('unsigned', 128, 18);
rescaleDecimalFixedPoint(evmUsdc('100.123456789012345678'), 64, 6, 'floor');
// represents 100.123456

Formatting

*ToString returns the canonical decimal string. Trailing zeros are trimmed by default.

decimalFixedPointToString(usdc('42.5')); // "42.5"
decimalFixedPointToString(usdc('-0.05')); // "-0.05"
binaryFixedPointToString(audioSample('0.5')); // "0.5"

Binary fixed-points always have a finite decimal expansion, but that expansion can be long (up to fractionalBits digits). Pass options.decimals with a RoundingMode to cap the output.

binaryFixedPointToString(audioSample('0.1')); // "0.0999755859375"
binaryFixedPointToString(audioSample('0.1'), { decimals: 4, rounding: 'round' }); // "0.1"

Set options.padTrailingZeros to emit exactly that many fractional digits.

decimalFixedPointToString(usdc('42.5'), { padTrailingZeros: true }); // "42.500000"

*ToNumber returns a JavaScript number. It is inherently lossy — prefer *ToString when exactness matters.

decimalFixedPointToNumber(usdc('42.5')); // 42.5

For locale-aware output, pass any Intl.NumberFormat instance to formatDecimalFixedPoint or formatBinaryFixedPoint. The helpers route the raw bigint through string scientific notation so precision is preserved beyond JavaScript's number mantissa.

const eurc = decimalFixedPoint('unsigned', 64, 6);
const eurFormatter = new Intl.NumberFormat('de-DE', { currency: 'EUR', style: 'currency' });
formatDecimalFixedPoint(eurFormatter, eurc('1234.5')); // "1.234,50 €"

The same helper exists for binary fixed-points.

const fr = new Intl.NumberFormat('fr-FR', { maximumFractionDigits: 4 });
formatBinaryFixedPoint(fr, audioSample('0.1')); // "0,1"

To plug a binary fixed-point into a custom formatter, convert it to its exact base-10 representation with binaryFixedPointToBase10 first. Decimal fixed-points already carry raw and decimals directly on the value object, so no equivalent helper is needed for them.

binaryFixedPointToBase10(audioSample('0.5')); // { raw: 500000000000000n, decimals: 15 }

Type guards

The is* and assertIs* guards narrow an unknown value to a fixed-point. All shape arguments are optional — pass undefined for any dimension you don't care about.

isDecimalFixedPoint(value); // any decimal fixed-point?
isBinaryFixedPoint(value, 'signed', 16, 15); // specifically an `audioSample`?
assertIsDecimalFixedPoint(value); // throws SHAPE_MISMATCH if not a decimal fixed-point

Codecs

Fixed-points also come with byte-level codecs compatible with the rest of the @solana/codecs ecosystem. The codec is a FixedSizeCodec whose size is totalBits / 8totalBits must therefore be a multiple of 8.

const codec = getBinaryFixedPointCodec('signed', 16, 15);
const bytes = codec.encode(audioSample('0.5')); // 0x0040
codec.decode(bytes); // represents 0.5

Values serialize in little-endian by default. Pass { endian: 'be' } for big-endian.

getDecimalFixedPointCodec('unsigned', 64, 6, { endian: 'be' }).encode(usdc('42.5'));
// 0x0000000002879b9a

Signed values use two's-complement and the codec accepts any byte-aligned width, including non-standard ones like 24, 40, 72, or 136 bits.

Separate get*Encoder and get*Decoder functions are also available.

const bytes = getDecimalFixedPointEncoder('unsigned', 64, 6).encode(usdc('42.5'));
const value = getDecimalFixedPointDecoder('unsigned', 64, 6).decode(bytes);

Types

  • BinaryFixedPoint<TSignedness, TTotalBits, TFractionalBits> — a binary fixed-point with mathematical value raw / 2 ** fractionalBits.
  • DecimalFixedPoint<TSignedness, TTotalBits, TDecimals> — a decimal fixed-point with mathematical value raw / 10 ** decimals.
  • Signedness'signed' | 'unsigned'.
  • RoundingMode'ceil' | 'floor' | 'round' | 'strict' | 'trunc'.
  • FixedPointToStringOptions — options accepted by *ToString (decimals, padTrailingZeros, rounding).
  • FixedPointCodecConfig — options accepted by the codec factories (endian).