Adds two floats.
Ok(Float) - The sum.Err(FloatError) - If addition fails.const a = Float.parse("1.5").value!;
const b = Float.parse("2.5").value!;
const result = a.add(b);
if (result.error) {
console.error(result.error);
}
assert(result.value.format() === "4");
Returns the 32-byte hexadecimal string representation of the float.
String - The 32-byte hex string.const float = Float.fromHex("0x0000000000000000000000000000000000000000000000000000000000000005").value!;
assert(float.asHex() === "0x0000000000000000000000000000000000000000000000000000000000000005");
Divides self by b.
Ok(Float) - The quotient.Err(FloatError) - If division fails.const a = Float.parse("6.0").value!;
const b = Float.parse("2.0").value!;
const result = a.div(b);
if (result.error) {
console.error(result.error);
}
assert(result.value.format() === "3");
Returns true if self is equal to b.
b - The Float value to compare with self.Ok(true) if self is equal to b.Ok(false) if self is not equal to b.Err(FloatError) if the comparison fails due to an error in the underlying EVM call or decoding.const a = Float.parse("2.0").value!;
const b = Float.parse("2.0").value!;
const result = a.eq(b);
if (result.error) {
console.error(result.error);
}
assert(result.value);
Returns the floor of the float.
Ok(Float) - The floored value.Err(FloatError) - If the operation fails.const x = Float.parse("3.75").value!;
const result = x.floor();
if (result.error) {
console.error(result.error);
}
assert(result.value.format() === "3");
Formats the float as a decimal string using default scientific notation range (1e-4 to 1e9).
Ok(String) - The formatted string.Err(FloatError) - If formatting fails.const floatResult = Float.parse("2.5");
if (floatResult.error) {
console.error(floatResult.error);
}
const float = floatResult.value;
const formatResult = float.format();
assert(formatResult.value === "2.5");
Formats the float as a decimal string with a custom scientific notation range.
scientific_min - Values smaller than this (in absolute value) use scientific notation.scientific_max - Values larger than this (in absolute value) use scientific notation.Ok(String) - The formatted string.Err(FloatError) - If formatting fails.const float = Float.parse("0.5").value!;
const min = Float.parse("1").value!;
const max = Float.parse("100").value!;
const result = float.formatWithRange(min, max);
assert(result.value === "5e-1");
Formats the float as a decimal string with explicit scientific notation control.
scientific - If true, always use scientific notation. If false, use decimal notation.Ok(String) - The formatted string.Err(FloatError) - If formatting fails.const float = Float.parse("123.456").value!;
const decResult = float.formatWithScientific(false);
assert(decResult.value === "123.456");
const sciResult = float.formatWithScientific(true);
assert(sciResult.value === "1.23456e2");
Returns the fractional part of the float.
Ok(Float) - The fractional part.Err(FloatError) - If the operation fails.const x = Float.parse("3.75").value!;
const result = x.frac();
if (result.error) {
console.error(result.error);
}
assert(result.value.format() === "0.75");
Returns true if self is greater than b.
b - The Float value to compare with self.Ok(true) if self is greater than b.Ok(false) if self is not greater than b.Err(FloatError) if the comparison fails due to an error in the underlying EVM call or decoding.const a = Float.parse("2.0").value!;
const b = Float.parse("1.0").value!;
const result = a.gt(b);
if (result.error) {
console.error(result.error);
}
assert(result.value);
Returns true if self is greater than or equal to b.
b - The Float value to compare with self.Ok(true) if self is greater than or equal to b.Ok(false) if self is not greater than or equal to b.Err(FloatError) if the comparison fails due to an error in the underlying EVM call or decoding.const a = Float.parse("2.0").value!;
const b = Float.parse("1.0").value!;
const result = a.gte(b);
if (result.error) {
console.error(result.error);
}
assert(result.value);
Returns the multiplicative inverse of the float.
Ok(Float) - The inverse.Err(FloatError) - If inversion fails.const x = Float.parse("2.0").value!;
const inv = x.inv();
if (inv.error) {
console.error(inv.error);
}
assert(inv.value.format().startsWith("0.5"));
Checks if the float is zero.
Ok(true) if the float is zero.Ok(false) if the float is not zero.Err(FloatError) if the operation fails.const zero = Float.parse("0").value!;
const result = zero.isZero();
if (result.error) {
console.error(result.error);
}
assert(result.value);
Returns true if self is less than b.
b - The Float value to compare with self.Ok(true) if self is less than b.Ok(false) if self is not less than b.Err(FloatError) if the comparison fails due to an error in the underlying EVM call or decoding.const a = Float.parse("1.0").value!;
const b = Float.parse("2.0").value!;
const result = a.lt(b);
if (result.error) {
console.error(result.error);
}
assert(result.value);
Returns true if self is less than or equal to b.
b - The Float value to compare with self.Ok(true) if self is less than or equal to b.Ok(false) if self is not less than or equal to b.Err(FloatError) if the comparison fails due to an error in the underlying EVM call or decoding.const a = Float.parse("1.0").value!;
const b = Float.parse("2.0").value!;
const result = a.lte(b);
if (result.error) {
console.error(result.error);
}
assert(result.value);
Returns the maximum of self and b.
b - The other Float to compare with.Ok(Float) - The maximum value.Err(FloatError) - If the operation fails.const a = Float.parse("1.0").value!;
const b = Float.parse("2.0").value!;
const result = a.max(b);
if (result.error) {
console.error(result.error);
}
assert(result.value.format() === "2");
Returns the minimum of self and b.
b - The other Float to compare with.Ok(Float) - The minimum value.Err(FloatError) - If the operation fails.const a = Float.parse("1.0").value!;
const b = Float.parse("2.0").value!;
const result = a.min(b);
if (result.error) {
console.error(result.error);
}
assert(result.value.format() === "1");
Multiplies two floats.
Ok(Float) - The product.Err(FloatError) - If multiplication fails.const a = Float.parse("2.0").value!;
const b = Float.parse("3.0").value!;
const result = a.mul(b);
if (result.error) {
console.error(result.error);
}
assert(result.value.format() === "6");
Returns the negation of the float.
Ok(Float) - The negated value.Err(FloatError) - If the operation fails.const x = Float.parse("3.14").value!;
const result = x.neg();
if (result.error) {
console.error(result.error);
}
assert(result.value.format() === "-3.14");
Subtracts b from self.
Ok(Float) - The difference.Err(FloatError) - If subtraction fails.const a = Float.parse("5.0").value!;
const b = Float.parse("2.0").value!;
const result = a.sub(b);
if (result.error) {
console.error(result.error);
}
assert(result.value.format() === "3");
Convert the float to a JS/TS bigint equivalent of asHex() returned hex string.
if conversion fails.
const float = Float.fromHex("0xfffffffe0000000000000000000000000000000000000000000000000000013a");
const value = float.toBigInt();
assert(value === 115792089183396302089269705419353877679230723318366275194376439045705909141818n);
Converts a Float to a fixed-point decimal value using the specified number of decimals.
decimals - The number of decimals in the fixed-point representation.Ok(String) - The resulting fixed-point decimal value as a string.Err(FloatError) - If the conversion fails.const float = Float.parse("123.45").value!;
const result = float.toFixedDecimal(2);
if (result.error) {
console.error(result.error);
}
assert(result.value === "12345");
Converts a Float to a fixed-point decimal value using the specified number of decimals lossy.
ToFixedDecimalLossyResult containing the value and lossless flag.
Tries to convert the float to a JS/TS bigint equivalent of asHex() returned hex string.
Ok(bigint) - The resulting bigint value.Err(FloatError) - If the conversion fails.const float = Float.fromHex("0xfffffffe0000000000000000000000000000000000000000000000000000013a");
const bigintResult = float.tryToBigInt();
if (bigintResult.error) {
console.error(bigintResult.error);
}
assert(bigintResult.value === 115792089183396302089269705419353877679230723318366275194376439045705909141818n);
StaticformatReturns the default maximum value for scientific notation formatting (1e9).
Ok(Float) - The default maximum (1e9).Err(FloatError) - If the EVM call fails.const maxResult = Float.formatDefaultScientificMax();
if (maxResult.error) {
console.error(maxResult.error);
}
const max = maxResult.value;
assert(max.format().value === "1000000000");
StaticformatReturns the default minimum value for scientific notation formatting (1e-4).
Ok(Float) - The default minimum (1e-4).Err(FloatError) - If the EVM call fails.const minResult = Float.formatDefaultScientificMin();
if (minResult.error) {
console.error(minResult.error);
}
const min = minResult.value;
assert(min.format().value === "0.0001");
StaticfromConstructs a Float from a bigint equivalent of the fromHex() returned Float.
if conversion fails.
const float = Float.fromBigint(115792089183396302089269705419353877679230723318366275194376439045705909141818n);
assert(float.asHex() === "0xfffffffe0000000000000000000000000000000000000000000000000000013a");
StaticfromConverts a fixed-point decimal value to a Float using the specified number of decimals.
value - The fixed-point decimal value as a string.decimals - The number of decimals in the fixed-point representation.Ok(Float) - The resulting Float value.Err(FloatError) - If the conversion fails.const floatResult = Float.fromFixedDecimal("12345", 2);
if (floatResult.error) {
console.error(floatResult.error);
}
const float = floatResult.value;
assert(float.format() === "123.45");
StaticfromConverts a fixed-point decimal value to a Float using the specified number of decimals,
allowing lossy conversions and reporting whether precision was preserved.
This function attempts to convert a fixed-point decimal representation to a Float.
Unlike fromFixedDecimal, this method will not fail if precision is lost during conversion,
but instead reports the loss through the lossless flag in the result.
value - The fixed-point decimal value as a bigint (e.g., 12345n for 123.45 with 2 decimals).decimals - The number of decimal places in the fixed-point representation (0-255).Returns a FromFixedDecimalLossyResult containing:
float - The resulting Float value.lossless - Boolean flag indicating whether the conversion preserved all precision (true) or was lossy (false).Throws a JsValue error if:
// Lossless conversion
const result = Float.fromFixedDecimalLossy(12345n, 2);
const float = result.float;
const wasLossless = result.lossless;
assert(float.format()?.value === "123.45");
assert(wasLossless === true);
// Potentially lossy conversion
const result2 = Float.fromFixedDecimalLossy(123456789012345678901234567890n, 18);
if (!result2.lossless) {
console.warn("Precision was lost during conversion");
}
StaticfromConstructs a Float from a 32-byte hexadecimal string.
hex - The 32-byte hex string to parse.Ok(Float) - The float parsed from the hex string.Err(FloatError) - If the hex string is not valid or not 32 bytes.const floatResult = Float.fromHex("0x0000000000000000000000000000000000000000000000000000000000000005");
if (floatResult.error) {
console.error(floatResult.error);
}
const float = floatResult.value;
assert(float.asHex() === "0x0000000000000000000000000000000000000000000000000000000000000005");
StaticmaxReturns the maximum negative value that can be represented as a Float.
Ok(Float) - The maximum negative value (closest to zero).Err(FloatError) - If the EVM call fails.const maxNegResult = Float.maxNegativeValue();
if (maxNegResult.error) {
console.error(maxNegResult.error);
}
const maxNeg = maxNegResult.value;
assert(!maxNeg.format().error);
StaticmaxReturns the maximum positive value that can be represented as a Float.
Ok(Float) - The maximum positive value.Err(FloatError) - If the EVM call fails.const maxPosResult = Float.maxPositiveValue();
if (maxPosResult.error) {
console.error(maxPosResult.error);
}
const maxPos = maxPosResult.value;
assert(!maxPos.format().error);
StaticminReturns the minimum negative value that can be represented as a Float.
Ok(Float) - The minimum negative value (furthest from zero).Err(FloatError) - If the EVM call fails.const minNegResult = Float.minNegativeValue();
if (minNegResult.error) {
console.error(minNegResult.error);
}
const minNeg = minNegResult.value;
assert(!minNeg.format().error);
StaticminReturns the minimum positive value that can be represented as a Float.
Ok(Float) - The minimum positive value.Err(FloatError) - If the EVM call fails.const minPosResult = Float.minPositiveValue();
if (minPosResult.error) {
console.error(minPosResult.error);
}
const minPos = minPosResult.value;
assert(!minPos.format().error);
StaticparseParses a decimal string into a Float.
str - The string to parse.Ok(Float) - The parsed float.Err(FloatError) - If parsing fails.const floatResult = Float.parse("3.1415");
if (floatResult.error) {
console.error(floatResult.error);
}
const float = floatResult.value;
assert(float.format() === "3.1415");
StatictryConstructs a Float from a bigint equivalent of the fromHex() returned Float.
Ok(Float) - The resulting Float value.Err(FloatError) - If the conversion fails.const floatResult = Float.tryFromBigint(115792089183396302089269705419353877679230723318366275194376439045705909141818n);
if (floatResult.error) {
console.error(floatResult.error);
}
const value = float.value;
assert(value.asHex() === "0xfffffffe0000000000000000000000000000000000000000000000000000013a");
StaticzeroReturns the zero value of a Float in its maximized representation.
Ok(Float) - The zero value.Err(FloatError) - If the EVM call fails.const zeroResult = Float.zero();
if (zeroResult.error) {
console.error(zeroResult.error);
}
const zero = zeroResult.value;
assert(zero.isZero().value);
assert(zero.format().value === "0");
Returns the absolute value of the float.
Returns
Ok(Float)- The absolute value.Err(FloatError)- If the operation fails.Example