toNumber β GTM Variable Template for String
toNumber CORE String
Converts the input into a number, or returns NaN if the input cannot be converted.
When to Use This
Section titled βWhen to Use ThisβString Manipulation
Transform, clean, and normalize text data for consistent downstream processing.
Type Conversion
Safely convert between data types β strings, numbers, booleans, arrays, objects.
Examples
Section titled βExamplesβ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
""
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.
toNumber
Value To Convert
πΎ The value to be converted into a number.
Supported formats:
β Any
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
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
toNumber()
Related Variables
Section titled βRelated VariablesβSame category: String
Under the Hood
Section titled βUnder the Hoodβπ 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'