parseCurrency β GTM Variable Template for Number
parseCurrency EXTENDED Number
Extracts and parses a valid number from a formatted currency string by stripping out currency symbols, thousand separators, and whitespace.
When to Use This
Section titled βWhen to Use ThisβNumber Operations
Arithmetic, rounding, clamping, and mathematical transformations on numeric values.
Formatting
Normalize casing, spacing, encoding, and presentation of data values.
Type Conversion
Safely convert between data types β strings, numbers, booleans, arrays, objects.
Examples
Section titled βExamplesβUS Format (Dot)
INPUT
String To Process: $ 1,234.56 USD
Decimal Separator: .
Decimal Separator: .
OUTPUT
1234.56
EU Format (Comma)
INPUT
String To Process: 1 234,56 β¬
Decimal Separator: ,
Decimal Separator: ,
OUTPUT
1234.56
German Format (Comma)
INPUT
String To Process: 1.234,56 β¬
Decimal Separator: ,
Decimal Separator: ,
OUTPUT
1234.56
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.
parseCurrency
String To Process
πΎ The currency string to parse.
Supported formats:
β String
Supported formats:
β String
Decimal Separator
βοΈ The decimal separator used in the source string. All other non-digit characters (currency symbols, spaces, thousand separators) will be stripped automatically.
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.
String To Process string
π‘ 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.
parseCurrency()
Related Variables
Section titled βRelated VariablesβSame category: Number
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/*** Extracts and parses a valid number from a formatted currency string.** @param {string} data.src - The currency string to parse.* @param {string} data.dec - The decimal separator used ('.' or ',').* @param {Function|string} [data.out] - Optional output handler.** Direct-mode specific parameters:* @param {Function} [data.pre] - Optional pre-processor function to transform src.** @returns {number|undefined} The parsed number, or undefined if invalid.** @framework ggLowCodeGTMKit*/const makeNumber = require('makeNumber');
const parseCurrency = function(input, decimalSeparator) { if (typeof input !== 'string') return undefined;
const sep = decimalSeparator === ',' ? ',' : '.';
let cleanString = ""; for (let i = 0; i < input.length; i++) { const char = input.charAt(i); if ( (char >= '0' && char <= '9') || char === '-' || char === sep ) { cleanString += char; } }
if (cleanString === "") return undefined;
if (sep === ',') { let dotString = ""; for (let i = 0; i < cleanString.length; i++) { dotString += cleanString.charAt(i) === ',' ? '.' : cleanString.charAt(i); } cleanString = dotString; }
const parsed = makeNumber(cleanString); if (parsed !== parsed) return undefined; // NaN check
return parsed;};const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);// ===============================================================================// parseCurrency - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);return out(parseCurrency(value, data.dec));// ===============================================================================// parseCurrency(...) β Apply Mode// ===============================================================================/*return function(value) { return out(parseCurrency(value, data.dec));};*/π§ͺ View Test Scenarios (6 tests)
β
'[example] US Format (Dot)'β
'[example] EU Format (Comma)'β
'[example] German Format (Comma)'β
Negative valuesβ
No numbers returns undefinedβ
Non-string returns undefined