rescaleDecimalFixedPoint

function rescaleDecimalFixedPoint<
    TSignedness,
    TNewTotalBits,
    TNewDecimals,
>(
    value,
    newTotalBits,
    newDecimals,
    rounding?,
): DecimalFixedPoint<TSignedness, TNewTotalBits, TNewDecimals>;

Returns a DecimalFixedPoint with the same signedness as value but a new totalBits and decimals. If the requested shape matches the input shape, the same reference is returned.

Scale-up (higher decimals) is always exact. Scale-down (lower decimals) is potentially lossy; the optional RoundingMode is consulted on inexact results and defaults to 'strict', which throws SOLANA_ERROR__FIXED_POINTS__STRICT_MODE_PRECISION_LOSS.

Throws SOLANA_ERROR__FIXED_POINTS__ARITHMETIC_OVERFLOW when the rescaled raw value does not fit the new totalBits.

Type Parameters

Type Parameter
TSignedness extends Signedness
TNewTotalBits extends number
TNewDecimals extends number

Parameters

ParameterType
valueDecimalFixedPoint<TSignedness, number, number>
newTotalBitsTNewTotalBits
newDecimalsTNewDecimals
rounding?RoundingMode

Returns

DecimalFixedPoint<TSignedness, TNewTotalBits, TNewDecimals>

Example

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

See

On this page