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.

Catalog variable to accept decimal values

sushma9
Tera Contributor

 Hi ,

I have variable called Cost and it should accept the  both decimal values and number. How can i achieve this.

 

Example :

1.Cost : 100 

2. cost : 1000.12345

 Note : It should accept both numbers and decimal values 

2 ACCEPTED SOLUTIONS

Prithvi_b
Tera Guru

Hi @sushma9 

function onSubmit() {
var cs = g_form.getValue('cost');
var letter = /^[0-9.]+$/;
if ((cs.match(letter))) {
var value = Math.round(cs*100)/100; // 10 defines 1 decimals, 100 for 2, 1000 for 3
//alert(value);
g_form.setValue('cost',value); //Please set value according to your need
} else {
// g_form.addErrorMessage('Add only numerics');
g_form.showErrorBox('cost','Add only numerics');
g_form.submitted = false;
return false;

}

}

 

 

Please modify the code for two decimal positions. Please find the attached screenshot.

Instead of var value = Math.round(cs*1000)/1000; , i gave var value = Math.round(cs*100)/100; .

 

Thankyou,

Prithvi

View solution in original post

Hi @sushma9 ,

PFA.

If you find my reply as correct,  mark as Correct.

Thankyou

 

View solution in original post

9 REPLIES 9

Hi Prithvi , 

Thanks for  your reply .

Its working fine almost 95% but what i need is when you enter 124.567 then it should come as 124.57 after the 6 the values is 7 so it should add 1 to 6 and has to come as 124.57. 

note : 

if any  value after 5 then it should add 1 to previsoue number  

ex : 400.009 , 346.127

out put : 400. 01 , 346.13

 

Prithvi_b
Tera Guru

Hi @sushma9 

function onSubmit() {
var cs = g_form.getValue('cost');
var letter = /^[0-9.]+$/;
if ((cs.match(letter))) {
var value = Math.round(cs*100)/100; // 10 defines 1 decimals, 100 for 2, 1000 for 3
//alert(value);
g_form.setValue('cost',value); //Please set value according to your need
} else {
// g_form.addErrorMessage('Add only numerics');
g_form.showErrorBox('cost','Add only numerics');
g_form.submitted = false;
return false;

}

}

 

 

Please modify the code for two decimal positions. Please find the attached screenshot.

Instead of var value = Math.round(cs*1000)/1000; , i gave var value = Math.round(cs*100)/100; .

 

Thankyou,

Prithvi

Hi @sushma9 ,

PFA.

If you find my reply as correct,  mark as Correct.

Thankyou

 

Hi ,

Thanks for your support .

Hi @Prithvi_b ,

 

Thanks for the script. It is working fine but there is a java script browser console error is coming which says "cs.match is not a function". How do I avoid this error. Please help!

Thank you in Advance.