Skip to content

toFixed — GTM Variable Template for Number

VARIABLES › NUMBER
toFixed CORE Number

Rounds a number to a fixed number of decimal places.


When to Use This

Number Operations

Arithmetic, rounding, clamping, and mathematical transformations on numeric values.


Examples

Round to 2 decimals
INPUT
Number To Round: 3.14159
Decimal Places: 2
OUTPUT
3.14
Trailing zeros added
INPUT
Number To Round: 10.5
Decimal Places: 3
OUTPUT
10.500

GTM Configuration

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
Decimal Places
🎯 Number of decimal places to keep.

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
toFixed()


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 ? data.dec : decimals;
   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