We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Client Script FX Currency calculations

Wade Clairmont
Tera Guru

I have this client script using FX Currency Fields:

 

    var unit_price = g_form.getValue('unit_price');
    g_form.addErrorMessage('FX ' + unit_price);

    var currencyCode = unit_price.split(';')[0];
    var unitCost = parseFloat(unit_price.split(';')[1]);

    g_form.addErrorMessage('unitcost ' + unitCost);
 
And the results

Why is this only taking the value left of the "," ?

 

If I use unitCost of USD;999.00, then it works just fine.


Thanks in advance,

Wade

2 REPLIES 2

pavani_paluri
Tera Guru

Hi @Wade Clairmont 

 

The FX currency field gives you a string like:

USD;999,00
JavaScript’s parseFloat() only understands numbers with a **dot** (`.`) as the decimal separator. When it sees a comma (`999,00`), it stops at `999` and ignores the rest. That’s why `USD;999.00` works fine, but `USD;999,00` doesn’t.

Before you parse the number, replace the comma with a dot:

var unit_price = g_form.getValue('unit_price');
var currencyCode = unit_price.split(';')[0];
var rawValue = unit_price.split(';')[1];

// change comma to dot
rawValue = rawValue.replace(',', '.');

var unitCost = parseFloat(rawValue);
g_form.addErrorMessage('Currency: ' + currencyCode + ', Unit Cost: ' + unitCost);

 

Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Pavani P

Hi @Wade Clairmont ,

 
Could you Please accept the solution if that solves your problem.
 
 
Kind Regards,
Pavani P