Skip to content

toNumber β€” GTM Variable Template for String

VARIABLES β€Ί STRING
toNumber CORE String
Direct (.tpl) Apply (.tpl)

Converts the input into a number, or returns NaN if the input cannot be converted.


String Manipulation

Transform, clean, and normalize text data for consistent downstream processing.

Type Conversion

Safely convert between data types β€” strings, numbers, booleans, arrays, objects.


String to number
INPUT
Value To Convert: 42
OUTPUT
42
Decimal string to number
INPUT
Value To Convert: 3.14
OUTPUT
3.14
Non-numeric returns NaN
INPUT
Value To Convert: hello
OUTPUT
""

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

toNumber
Value To Convert
πŸ’Ύ The value to be converted into a number.

Supported formats:
  βœ“ Any
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.
Value To Convert any
πŸ’‘ Type any text to see the result update live
🎯 Using special value β€” click input to type instead
Test with:
Falsy
Truthy
toNumber()


πŸ“œ View Implementation Code
/**
* Converts the input into a number.
*
* @param {any} data.src - The value to be converted into a number.
* @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 conversion.
*
* @returns {number} The converted number, or NaN if the input cannot be converted.
*
* @framework ggLowCodeGTMKit
*/
const makeNumber = require('makeNumber');
const toNumber = function(input) {
return makeNumber(input);
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// toNumber - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(toNumber(value));
// ===============================================================================
// toNumber() – Apply Mode
// ===============================================================================
/*
return function(value) {
return out(toNumber(value));
};
*/
πŸ§ͺ View Test Scenarios (5 tests)
βœ… '[example] String to number'
βœ… '[example] Decimal string to number'
βœ… Already a number - should return unchanged
βœ… Boolean true - should convert to 1
βœ… '[example] Non-numeric returns NaN'