Skip to content

parseFloatValue — GTM Variable Template for Number

VARIABLES › NUMBER
parseFloatValue CORE Number

Attempts to convert a given value into a floating-point number. It extracts the numeric part from the start of the string and stops when it encounters a non-numeric character.


When to Use This

Number Operations

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

Type Conversion

Safely convert between data types — strings, numbers, booleans, arrays, objects.

Extraction

Pull specific values, segments, or patterns from complex data structures.


Examples

Parse float string
INPUT
Value To Convert: 3.14
OUTPUT
3.14
Extract leading number
INPUT
Value To Convert: 12.5abc
OUTPUT
12.5
Non-numeric returns NaN
INPUT
Value To Convert: abc123
OUTPUT
true

GTM Configuration

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

parseFloatValue
Value To Convert
Converts a value to a floating-point number. Extracts the numeric part from the start of a string.

*** Parse float string***
Input: 3.14
↪️ Output: 3.14

*** Extract leading number***
Input: 12.5abc
↪️ Output: 12.5

*** Non-numeric returns NaN***
Input: abc123
↪️ Output: true
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 number
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
parseFloatValue()


Under the Hood

📜 View Implementation Code
/**
 * Attempts to convert a given value into a floating-point number.
 * It extracts the numeric part from the start of the string and stops when it encounters a non-numeric character.
 *
 * @param {any} data.src - The value to be converted to a float.
 * @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 parsing.
 * 
 * @returns {number} The parsed floating-point number, or NaN if the value cannot be converted.
 *
 * @framework ggLowCodeGTMKit
 */

const makeNumber = require('makeNumber');
const getType = require('getType');

const parseFloatValue = function(input) {
    const safeNaN = 0/0;
    if (input == null) { 
        return safeNaN; 
    }
    const inputString = input.toString();
    if (getType(inputString.match("^(\\d+\\.?\\d*)")) !== "null") {
        return makeNumber(inputString.match("^(\\d+\\.?\\d*)")[0]);
    }
    return safeNaN;
};

const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);

// ===============================================================================
// parseFloatValue - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(parseFloatValue(value));

// ===============================================================================
// parseFloatValue() – Apply Mode
// ===============================================================================
/*
return function(value) {
   return out(parseFloatValue(value));
};
*/
🧪 View Test Scenarios (5 tests)
✅ '[example] Parse float string'
✅ '[example] Extract leading number'
✅ Integer string - should parse to integer (as float)
✅ '[example] Non-numeric returns NaN'
✅ Null input - should return NaN