Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to convert price datatype field to float ?

loteodor
Kilo Contributor


Hi,

I need to write a Client Script that get the value from a form field that has a Price datatype and convert it into a Float Number.

The field is called 'Unit_Cost' and I need to retrieve the value in order to multiply it with an other number called Quantity.

I wrote a simple script above, but I get   a problem retrieving the value of the Unit Cost field. If I insert a value of 4digits (es. 1.000 ) my code trunk the value after the '.' (es. 1.000 -> 1).

----------------------------------------------------------------

var split = (g_form.getValue('Unit_Cost).split(";")); //retrieve the String cause it has a Price datatype in the form of "€;00.00", and split the string in two parts

var temp=split[1]; // take the numeric part

    var costo = parseFloat(temp); //parse to Float the string -> here if the number is 1.000   --> it saves in costo: 1.

   

    var quantita = g_form.getIntValue('Quantity');

    var acquisizione_HW =   quantita*costo; //WRONG RESULT

----------------------------------------------------------------------

Can anyone explain to me how to get the correct Unit Cost floating number from the string ?

Thank you,

Lorenzo

2 REPLIES 2

NishantAkella
Giga Contributor

Hi Lorenzo,



Did you try using g_form.getDecimalValue function?



You don't have to retrieve using getValue() and then use the split function.



var unitCost = g_form.getDecimalValue("unit_cost"); // This should give you the right result.



-Nishant.


chris_jones
Kilo Explorer

I think it is likely this has not worked because you have not converted to String before parsing.

Do the following:

var split = (g_form.getValue('Unit_Cost).split(";"));

var temp=split[1]; // take the numeric part

var costo = parseFloat(temp.toString());    

var quantita = g_form.getIntValue('Quantity');
var acquisizione_HW =   quantita*costo;