decimalFixedPoint

function decimalFixedPoint<TSignedness, TTotalBits, TDecimals>(
    signedness,
    totalBits,
    decimals,
): (
    input,
    rounding?,
) => DecimalFixedPoint<TSignedness, TTotalBits, TDecimals>;

Returns a factory that constructs DecimalFixedPoint values from decimal strings.

The outer call validates the shape parameters once and the returned factory can be called many times to construct values of that shape.

If the string carries more precision than the target decimals can represent exactly, the returned factory throws SOLANA_ERROR__FIXED_POINTS__STRICT_MODE_PRECISION_LOSS under the default 'strict' rounding mode. Pass a different RoundingMode to allow a rounded result.

Type Parameters

Type Parameter
TSignedness extends Signedness
TTotalBits extends number
TDecimals extends number

Parameters

ParameterType
signednessTSignedness
totalBitsTTotalBits
decimalsTDecimals

Returns

(input, rounding?): DecimalFixedPoint<TSignedness, TTotalBits, TDecimals>;

Parameters

ParameterType
inputstring
rounding?RoundingMode

Returns

DecimalFixedPoint<TSignedness, TTotalBits, TDecimals>

Example

const usdc = decimalFixedPoint('unsigned', 64, 6);
usdc('42.5');          // raw === 42500000n
usdc('0.0000001');     // throws under the default 'strict' mode
usdc('0.0000001', 'round'); // raw === 0n

See

On this page