Skip to content

parseCurrency β€” GTM Variable Template for Number

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

Extracts and parses a valid number from a formatted currency string by stripping out currency symbols, thousand separators, and whitespace.


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.


US Format (Dot)
INPUT
String To Process: $ 1,234.56 USD
Decimal Separator: .
OUTPUT
1234.56
EU Format (Comma)
INPUT
String To Process: 1 234,56 €
Decimal Separator: ,
OUTPUT
1234.56
German Format (Comma)
INPUT
String To Process: 1.234,56 €
Decimal Separator: ,
OUTPUT
1234.56

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


πŸ“œ 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