Type/to search
Built-in Functions
Structures
Built-in Variables and Constants
Conventions
Exchanges
Capability Matrix
Centralized Exchanges
Decentralized Exchanges (DEX)
Prediction Markets
Securities and Futures
On-chain & Custom

Converts an on-chain integer value into a human-readable amount. The entire conversion is performed as exact string-based arithmetic without any floating-point operations, avoiding precision loss.

exchange.IO(k, amount, decimals)

Examples

Amount conversion for USDC and ETH.

javascript
function main() { var USDC = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48" // Human-readable amount -> on-chain integer; the precision can be passed as a number or as a // token address (decimals is read automatically) Log(exchange.IO("toUnits", "1.5", 6)) // Output: 1500000 Log(exchange.IO("toUnits", "1.5", USDC)) // Output: 1500000 Log(exchange.IO("toUnits", "0.01", 18)) // Output: 10000000000000000 // On-chain integer -> human-readable amount Log(exchange.IO("fromUnits", "1500000", 6)) // Output: 1.5 var raw = exchange.IO("api", USDC, "balanceOf", exchange.IO("address")) Log("USDC balance:", exchange.IO("fromUnits", raw, USDC)) // An error is reported when the number of decimal places exceeds the precision; no silent // truncation occurs Log(exchange.IO("toUnits", "1.0000001", 6), GetLastError()) }

Returns

TypeDescription

string

Returns the human-readable amount (a decimal string with trailing zeros removed).

Arguments

NameTypeRequiredDescription

k

string

Yes

Function of the exchange.IO() function. When set to fromUnits, it indicates that the function is used to convert an on-chain integer value into a human-readable amount.

amount

string / number

Yes

On-chain integer value to be converted. It can be a decimal string, a hexadecimal string prefixed with 0x, or a number.

decimals

number / string

Yes

Precision. You can pass a number (e.g., 6 for USDC, 18 for ETH) or a token contract address, in which case the decimals() method of that token contract will be called automatically to read the precision.

See Also

Remarks

This is the inverse operation of exchange.IO("toUnits", ...).