toFixed β GTM Variable Template for Number
When to Use This
Section titled βWhen to Use ThisβFormatting
Normalize casing, spacing, encoding, and presentation of data values.
Examples
Section titled βExamplesβRound to 2 decimals
INPUT
Number To Round: 3.14159
Decimal Places: 2
Decimal Places: 2
OUTPUT
3.14
Trailing zeros added
INPUT
Number To Round: 10.5
Decimal Places: 3
Decimal Places: 3
OUTPUT
10.500
Live Sandbox
Section titled βLive SandboxβThis is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
toFixed
Number To Round
πΎ The number to round to fixed decimal places.
Supported formats:
β Number
β String
Supported formats:
β Number
β String
Decimal Places
π― Number of decimal places to keep.
Supported formats:
β Number
β String
Supported formats:
β Number
β String
Input Setup
Input Function (optional)
βοΈ Optional pre-processing function applied to the input before internal logic (e.g., convert object to string, normalize case). Internal transformations such as case handling will still apply afterward.
Result Handling
Output Function (optional)
βοΈ Optional function to apply to the result before returning it (e.g., str => str + ' β¬', val => val !== undefined for boolean conversion). Useful for chaining transformations on the output.
Number To Round number
π‘ Type any text to see the result update live
π― Using special value β click input to type instead
Test with:
Falsy
Truthy
Decimal Places number
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
toFixed()
Related Variables
Section titled βRelated VariablesβSame category: Number
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/*** Rounds a number to a fixed number of decimal places using Math.round.** @param {number} data.src - The number to round.* @param {number|string} data.dec - Number of decimal places to keep.* @param {Function|string} [data.out] - Optional output handler: function to transform result or string with format.** Direct-mode specific parameters:* @param {Function} [data.pre] - Optional pre-processor function to transform src before rounding.** @returns {string} The rounded number as a string with fixed decimals.** @framework ggLowCodeGTMKit*/const Math = require('Math');const makeNumber = require('makeNumber');
const toFixed = function(num, decimals) { const numValue = makeNumber(num); const decValue = makeNumber(decimals); const factor = Math.pow(10, decValue); const rounded = Math.round(numValue * factor) / factor; const parts = rounded.toString().split('.'); const integerPart = parts[0]; let decimalPart = parts[1] || ''; while (decimalPart.length < decValue) { decimalPart += '0'; } if (decValue === 0) { return integerPart; } return integerPart + '.' + decimalPart;};const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);// ===============================================================================// toFixed - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);return out(toFixed(value, data.dec));// ===============================================================================// toFixed(...) β Apply Mode// ===============================================================================/*return function(value, decimals) { decimals = data.rp1 ? decimals : data.dec; return out(toFixed(value, decimals));};*/π§ͺ View Test Scenarios (5 tests)
β
'[example] Round to 2 decimals'β
Valid number with 0 decimal places - returns integerβ
'[example] Trailing zeros added'β
String number with decimal places - converts and roundsβ
Integer with decimal places - adds decimal point and zeros