exchange.IO("fromUnits", ...)
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
| Type | Description |
string | Returns the human-readable amount (a decimal string with trailing zeros removed). |
Arguments
| Name | Type | Required | Description |
k | string | Yes | Function of the |
amount | string / number | Yes | On-chain integer value to be converted. It can be a decimal string, a hexadecimal string prefixed with |
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 |
See Also
Remarks
This is the inverse operation of exchange.IO("toUnits", ...).