Javascript: Convert string to number but keep decimal

e_wilber
Tera Guru

I have a currency field that stores a value in this format: 'USD;1234.05' so it's storing as a string.

I am splitting on ; to just get the number returned (as a string).
var amnt = newValue.split(/;/);

My question is now that the number is a string (in this case it is 1234.05), how do I convert it to a number and keep the decimal?

I have tried:

var str = '11,110.05';
gs.print('String: ' + str);

var strInt = parseInt(str);
gs.print('Int: ' + strInt);

var flt = parseFloat(str).toFixed(2);
gs.print('Float: ' + flt);

Which is returning

*** Script: String: 11,110.05
*** Script: Int: 11
*** Script: Float: 11.00

Int and Float are changing the number completely. I just need the exact number and decimal to come through so I can compare it to another number. In the real example I want 11,110.05 returned as a number so I can compare it to 5000 to see if it's greater than 5,000.

1 ACCEPTED SOLUTION

-O-
Kilo Patron
Kilo Patron

Not sure if it is supported in Portal (or Workspace), but in Platform UI (UI16/Polaris) GlideForm has a method to return the value as a Number:

g_form.getDecimalValue('<currency field name>');

View solution in original post

6 REPLIES 6

-O-
Kilo Patron
Kilo Patron

Not sure if it is supported in Portal (or Workspace), but in Platform UI (UI16/Polaris) GlideForm has a method to return the value as a Number:

g_form.getDecimalValue('<currency field name>');

-O-
Kilo Patron
Kilo Patron

I'm assuming this a Client Script environment.

-O-
Kilo Patron
Kilo Patron

As for the problem with the original solution: both parsers stop parsing at the 1st illegal character. Since JavaScript only recognizes the dot as decimal separator and does not use grouping characters, both will stop parsing before the comma grouping separator in your string.

You could make use of the global variables g_user_decimal_separator and g_user_grouping_separator to eliminat the latter and transform the former into dot to convert the number display value into a parse-able one, but I think using newValue to extract the currency code and the above mentioned API to get the numeric value is simpler.

How would that work for

let fltStr = '1,200.11';

?