convert floating point numbers

CandyDee
Kilo Sage

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.

 

2 ACCEPTED SOLUTIONS

Allen Andreas
Administrator
Administrator

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!

View solution in original post

@Allen A You mean

gr.setValue('name_of_float_field', newNum);

(and not

gr.setValue('name_of_float_field');

), right? 🙂

View solution in original post

7 REPLIES 7

CandyDee
Kilo Sage

Thank you both, Ill have to take it away and chat to the bosses and see what they prefer, but again that you for the speedy response and filling in my knowledge gap.

Hi @CandyDee 

Please mark my reply above as Correct as you ended up using my using my script (or will) right?

Currently, you have another post marked as Correct for my script?

If you don't mind, I feel my response explained several things and assisted with providing a script to work your issue through to resolution for current records and their related float values. That other post was even deleted from this thread as I had made my post.

Let me know if you need nay other help.

Thank you very much! 🙂


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Sorted, thank you A Allen