circle-info
This is the first version of our Documentation, it will be improved and extended soon.
Page cover

Decimal Precision & Formatting

IFÁ Labs uses fixed-point arithmetic for onchain price storage, a common pattern in smart contracts to represent decimal numbers without floating-point operations (which Solidity does not natively support).

Key Fields Returned by getAssetInfo

Field
Type
Description

price

int256

The scaled price value (integer)

decimal

int8

Negative exponent indicating the scaling factor (e.g., -18)

lastUpdateTime

uint256

Timestamp of the update

How Scaling Works

The actual human-readable price is calculated as:

Human Price = price ÷ 10^(-decimal)

  • Most stablecoin feeds use decimal = -18 → prices scaled by 10^18.

  • Example: For a $1.00 pegged stablecoin:

    • price = 1,000,000,000,000,000,000 (1e18)

    • decimal = -18

    • Human price = 1e18 / 10^18 = 1.00

Solidity Example: Converting to Human-Readable

Since Solidity lacks native division by powers of 10 with variable exponents, handle scaling carefully (often offchain or with libraries like PRBMath for onchain).

// Offchain or simple onchain (assuming fixed -18 decimals)
uint256 humanPrice = uint256(info.price) / 10**18;

// For variable decimals (advanced — use a library)
function toHuman(int256 _price, int8 _decimal) public pure returns (uint256) {
    require(_decimal < 0, "Positive decimals not supported");
    uint8 decimals = uint8(-_decimal);
    return uint256(_price) / (10 ** decimals);
}

Best Practices

  • Assume -18 for stablecoins unless checking dynamically.

  • Use unsigned for positive prices; prices are always positive in current feeds.

  • Libraries for advanced math → PRBMath or ABDKMath for safe fixed-point operations.

This consistent scaling ensures precise, gas-efficient calculations in your protocols.

This completes the Core Concepts section.

Next: Explore the API Reference for full contract details.

Last updated

Was this helpful?