Skip to content

Intro to the number methods

Note

In the function editor, you can perform any JS method, using any value from cookies, variables, parameters, request responses, inputs or forms. Make sure to use the parameters correctly.

toFixed()

Description Formats a number with a fixed number of digits after the decimal point. Ideal for currency, measurements, or consistent decimal displays. number-fixed-example

javascript
/* Keep in mind that this example has fixed values, however,
you can replace any value with a value from a cookie, variable, form, input, etc. */
const price = 5.567;
return price.toFixed(2); // Result: "5.57" (rounded to 2 decimals)

toPrecision()

Description Formats a number to a specified length of significant digits. Useful for scientific data or limiting display length. number-precision-example

javascript
/* Keep in mind that this example has fixed values, however,
you can replace any value with a value from a cookie, variable, form, input, etc. */
const value = 123.456;
return value.toPrecision(4); // Result: "123.5" (4 significant digits)

toString()

Description Converts a number to a string, optionally in a specific base (e.g., binary, hexadecimal). Helpful for encoding or UI displays. number-toString-example

javascript
/* Keep in mind that this example has fixed values, however,
you can replace any value with a value from a cookie, variable, form, input, etc. */
const decimalNumber = 255;
return decimalNumber.toString(16); // Result: "ff" (hexadecimal representation)

Number.parseInt()

Description Parses a string and returns an integer in a specified base (default: base 10). Ignores non-numeric suffixes. number-parseInt-example

javascript
/* Keep in mind that this example has fixed values, however,
you can replace any value with a value from a cookie, variable, form, input, etc. */
const pixelValue = '42px';
return Number.parseInt(pixelValue); // Result: 42

Number.parseFloat()

Description Parses a string and returns a floating-point number. Extracts decimals and ignores non-numeric suffixes. number-parseFloat-example

javascript
/* Keep in mind that this example has fixed values, however,
you can replace any value with a value from a cookie, variable, form, input, etc. */
const userInput = '3.14meters';
return Number.parseFloat(userInput); // Result: 3.14

Number.isInteger()

Description Checks if a value is an integer. Returns true or false. Useful for validation (e.g., age, quantity inputs). number-isInteger-example

javascript
/* Keep in mind that this example has fixed values, however,
you can replace any value with a value from a cookie, variable, form, input, etc. */
const value1 = 42;
const value2 = 42.5;
return Number.isInteger(value1); // true Number.isInteger(value2); // false

Number.isNaN()

Description Checks if a value is NaN ("Not-a-Number"). Safer than the global isNaN() (doesn’t coerce non-number types). number-isNaN-example

javascript
/* Keep in mind that this example has fixed values, however,
you can replace any value with a value from a cookie, variable, form, input, etc. */
const invalidCalc = 0 / 0;
return Number.isNaN(invalidCalc); // true