- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2021 02:29 PM
I have a few floating point integer fields on a form that are already populated with values.
So currently I have - total_cost 1200.3255
Ideally it display as 1200.32 but I cant change the field type.
Thank you all.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2021 02:46 PM
Hi,
Unfortunately, you aren't able to change the field type without data loss. So you'd have to either create a new field to capture this data in the type you'd like...or...remove values for this field on all records (which isn't ideal). You may be better suited to create a new field if you already have tons of record with data here.
Otherwise, you could write a fix script/background script to go through all records and clear it up to 2 decimals. Background script like this would help:
var gr = new GlideRecord('name_of_table');
gr.addQuery('name_of_float_field', '!=', '');
gr.query();
while (gr.next()) {
var float = gr.getValue('name_of_float_field');
var num = Number(float);
var newNum = num.toFixed(2);
gr.setValue('name_of_float_field', newNum);
gr.update();
}
So you'd want to supply your table name in appropriate spot and then your float field in the other 3 spots...this will check first to make sure it has a value now (so the script doesn't run unnecessarily) and then convert the current value from float to 2 decimal and then set the value back.
The above is an example, please take it from here. Consider referencing GlideRecord documentation if needed: https://developer.servicenow.com/dev.do#!/reference/api/orlando/server/no-namespace/c_GlideRecordSco...
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2021 02:56 PM
gr.setValue('name_of_float_field', newNum);
(and not
gr.setValue('name_of_float_field');
), right? 🙂

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2021 02:46 PM
Hi,
Unfortunately, you aren't able to change the field type without data loss. So you'd have to either create a new field to capture this data in the type you'd like...or...remove values for this field on all records (which isn't ideal). You may be better suited to create a new field if you already have tons of record with data here.
Otherwise, you could write a fix script/background script to go through all records and clear it up to 2 decimals. Background script like this would help:
var gr = new GlideRecord('name_of_table');
gr.addQuery('name_of_float_field', '!=', '');
gr.query();
while (gr.next()) {
var float = gr.getValue('name_of_float_field');
var num = Number(float);
var newNum = num.toFixed(2);
gr.setValue('name_of_float_field', newNum);
gr.update();
}
So you'd want to supply your table name in appropriate spot and then your float field in the other 3 spots...this will check first to make sure it has a value now (so the script doesn't run unnecessarily) and then convert the current value from float to 2 decimal and then set the value back.
The above is an example, please take it from here. Consider referencing GlideRecord documentation if needed: https://developer.servicenow.com/dev.do#!/reference/api/orlando/server/no-namespace/c_GlideRecordSco...
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2021 02:56 PM
gr.setValue('name_of_float_field', newNum);
(and not
gr.setValue('name_of_float_field');
), right? 🙂

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2021 02:56 PM
Thanks
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2021 02:54 PM
The question is: do you only want to display with 2 decimals, or do you also want to have the value returned with 2 decimals?
If you are OK with also computing (not just displaying) with 2 decimals, you can add an attribute to the field's dictionary: scale=2.