Skip to content

toFixedNumber β€” GTM Variable Template for Number

VARIABLES β€Ί NUMBER
toFixedNumber EXTENDED Number
Direct (.tpl) Apply (.tpl)

Rounds a number to a fixed number of decimal places and returns it as a number.


Formatting

Normalize casing, spacing, encoding, and presentation of data values.


Round to 2 decimals
INPUT
Number To Round: 3.14159
Decimal Places: 2
OUTPUT
3.14
Round to integer
INPUT
Number To Round: 10.5
Decimal Places: 0
OUTPUT
11

This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.

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


πŸ“œ 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 {number} The rounded number.
*
* @framework ggLowCodeGTMKit
*/
const Math = require('Math');
const makeNumber = require('makeNumber');
const toFixedNumber = function(num, decimals) {
const numValue = makeNumber(num);
const decValue = makeNumber(decimals);
if (numValue !== numValue || decValue !== decValue) { return undefined; }
const factor = Math.pow(10, decValue);
return Math.round(numValue * factor) / factor;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// toFixedNumber - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(toFixedNumber(value, data.dec));
// ===============================================================================
// toFixedNumber(...) – Apply Mode
// ===============================================================================
/*
return function(value, decimals) {
decimals = data.rp1 ? decimals : data.dec;
return out(toFixedNumber(value, decimals));
};
*/
πŸ§ͺ View Test Scenarios (4 tests)
βœ… '[example] Round to 2 decimals'
βœ… '[example] Round to integer'
βœ… String number converts and rounds
βœ… Invalid inputs return undefined