Internally, we use Arbitrary-precision arithmetic (also known as Big Number arithmetic) to allow for the handling of large numbers and different assets.
On the Fuel network, we work with 9 decimals to represent amounts under a unit. This differs from chain to chain, so it is important to know the number of decimals used on the chain you are working with.
Note: The package
@fuels/assets
provides a list of assets and their decimals.
Below we will go over some common use cases for unit conversion.
Using our BN
class we can instantiate these numbers.
// #import { BN };
const result = new BN('100000000').toString();
// "100000000"
Or using our bn
utility function.
// #import { bn };
const result = bn('100000000').toString();
// "100000000"
Generally, we will need to convert u64
and u256
numbers to a BN
object when passing them to a Sway program from JavaScript. More information on this can be found here .
const MAX_U64 = bn('18446744073709551615');
const { waitForResult } = await contract.functions.echo_u64(MAX_U64).call();
const { value } = await waitForResult();
value.toString();
// "18446744073709551615"
Note: If a contract call returns a number that is too large to be represented as a JavaScript number, you can convert it to a string using the
toString
method instead oftoNumber
.
Parsing string-represented numbers (from user input) has never been easier, than using the parseUnits
function.
const result = bn.parseUnits('0.000000001').toString();
// "1"
We can parse large numbers.
const result = bn.parseUnits('100100').toString();
// "100100000000000"
Or numbers formatted for human readability.
const result = bn.parseUnits('100,100.000200001').toString();
// "100100000200001"
We can also parse numbers in other units of measure.
// #import { bn, DECIMAL_GWEI };
const result = bn.parseUnits('1', DECIMAL_GWEI).toString();
// "1000000000"
We can format common units of measure using the format
function.
In the following example, we format a BigNumber representation of one Gwei, into units for the Fuel network (with 3 decimal place precision).
const oneGwei = bn('1000000000');
const result = oneGwei.format();
// "1.000"
We can also format numbers in other units of measure by specifying the units
variable.
// #import { bn, DECIMAL_GWEI };
const twoGwei = bn('2000000000');
const result = twoGwei.format({ units: DECIMAL_GWEI });
// "2.000"
A precision
variable will allow for the formatting of numbers with a specific number of decimal places.
const oneGwei = bn('1000000000');
const result = oneGwei.format({ precision: 1 });
// "1.0"
The formatUnits
function is a lesser alternative to the format
function, as it will maintain the same precision as the input value.
const oneGwei = bn('1000000000');
const result = oneGwei.formatUnits();
// "1.000000000"
We can also format numbers in other units of measure by specifying the units
variable.
// #import { bn, DECIMAL_KWEI };
const oneKwei = bn('1000000000000000');
const result = oneKwei.formatUnits(DECIMAL_KWEI);
// "1.000000000000000"