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 Limit the Number of decimal places to "two" for Price field

Bhavani Yavvar2
Giga Contributor

Hi ,
I am trying to limit the number of decimal places to "two" for price field by using "Scale" attribute to the field.

In the Dictionary configuration, I want to restrict the user should not enter more than 2 decimal places.

The decimal places are a combination of ‘Max length’ and the ‘scale’ attribute.

For the field, the scale= 2 and Modified the Maximum length as "22". (As the maximum length is 20 by default + scale)

 

Found 1 KB Article, Number of decimal places always rounds to 2 even when 'scale' is attribute is set - Known Error (ser...

 

Is any way to limit the 2 decimal places.

 

Thanks in Advance,

Bhavani Yavvari

2 REPLIES 2

Jhansi Kollipar
Tera Expert

Hi @Bhavani Yavvar2 ,

 

Scale attribute works only for decimal type field.Since you are using price type field, try to do it with a client script. Please find the below onchange client script which onruns onchange of the price type field.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

//Type appropriate comment here, and begin script below
var sp=newValue.split(';'); //since you will be getting value which includes currency name as well for example(US;8.9)
var ps=sp[1].toString();
ps=ps.split('.');
var ps2=ps[1];
if(ps2.length>2){
var x =Number(sp[1]);
var round=Math.round(x*100)/100;
alert(round);
g_form.setValue('u_test',sp[0]+';'+round);
}

}

 

if it helps.Mark helpful

 

dgarad
Giga Sage

var num = 5.56789;
var n = num.toFixed(2);

If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad