parseFloat ignoring symbols like "." on values

Felipe Angelo d
Tera Contributor

Hello guys!

I have a script for a multirow, where when I leave the checkbox as true, it does the sum in the Total Value field, however, the numbers that are being added have decimal values, such as 100,000.50, but at the time of sum, it ignores everything after the dot. I'll leave some prints here to facilitate understanding.

find_real_file.png

I'm using this code.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var valorInserido;
    var somando = 0;
    var i = 1;
	
    try {
        while (top.document.getElementById('mrvs_ir_line').getElementsByTagName("tr")[i].getElementsByTagName("td")[2].innerText != '') {
            valorInserido = parseFloat(top.document.getElementById('mrvs_ir_line').getElementsByTagName("tr")[i].getElementsByTagName("td")[2].innerText);
            somando += parseFloat(valorInserido);
            
            i++;
        }
    } catch (x) {
        g_form.setValue("vs_valor_total", somando);
    }
    g_form.setValue("vs_valor_total", somando);
}

What can I do to make it sum the values ​​correctly?

 

Regards.

3 REPLIES 3

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Felipe,

It's probably because the field type is "Single Line Text".

To show 2 decimal points use .toFixed()

e.g.

g_form.setValue("vs_valor_total", somando.toFixed(2));  // 2 decimal point

Hi Hitoshi

I made the change on the code, but it didn't work, here a print

find_real_file.png

The field "Value" needs all the numbers and it format have a regex set as BR Currency (Brazillian Real), could this be it?

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var valorInserido;
    var somando = 0;
    var i = 1;
	
    try {
        while (top.document.getElementById('mrvs_ir_line').getElementsByTagName("tr")[i].getElementsByTagName("td")[2].innerText != '') {
            valorInserido = parseFloat(top.document.getElementById('mrvs_ir_line').getElementsByTagName("tr")[i].getElementsByTagName("td")[2].innerText);
            somando += parseFloat(valorInserido);
            
            i++;
        }
    } catch (x) {
        g_form.setValue("vs_valor_total", somando.toFixed(2));
    }
    g_form.setValue("vs_valor_total", somando.toFixed(2));
}

Tony Chatfield1
Kilo Patron

Hi, your screenshot shows text with a format of '100.000,50' and your post indicates a format of '100,000.50' and I believe that your results are correct based on either format.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat

If parseFloat encounters a character other than a plus sign (+), minus sign (- U+002D HYPHEN-MINUS), numeral (0–9), decimal point (.), or exponent (e or E), it returns the value up to that character, ignoring the invalid character and characters following it.

You should be able to test\validate this quickly in a background window.

var test1 = '100,000.50';
var test2 = '100,000.50';

//test1 = test1.replace(',','');
//test2 = test2.replace(',','');

var test3 = parseFloat(test1) + parseFloat(test2);

gs.info("Test3 " + test3);


var testA = '100.000,50';
var testB = '100.000,50';

//testA = testA.replace(',','');
//testB = testB.replace(',','');

var testC = parseFloat(testA) + parseFloat(testB);

gs.info("testC " + testC);