How to do Aritmetical Operations with values Obtained from Calculations Fields

Confluence User - 29 Aug, 2017

Hi Everybody, i´m taken the value of a couple of calculations fields from differents forms , but when try to do an algebraical operation the result is a value like NaN, when evaluate the type the value that i get from the fields i note that it is a string, so i decide use the function Number() to try to convert the value but it don´t work.

the question is: really it is the expected behavior when i get values from calculations fields? , in other hand why the function Number() don´t convert the values correctly?

 

 

<h3>Nota: Todo abono a la factura será revisado por el departamento de finanzas. .</h3>
<!-- Javascript JQuery to substring string -->

<script type = "text/javascript" >

 

var field1 = Number(FormUtil.getField("totalmonto"));
//var a = Number(field1.val());

// function updateField(){
var amount = 0
//amount = $(FormUtil.getField("totalmonto")).val() - $(FormUtil.getField("abono")).val();
amount = $(FormUtil.getField("totalmonto")).val() - 50 ;
// var amount = $(FormUtil.getField("totalmonto")).val();

$(FormUtil.getField("calculo")).val(amount);

// }

$(function(){

$(FormUtil.getField("totalmonto")).change(function(){
updateField();
});

$(FormUtil.getField("total_abonado")).change(function(){
updateField();
});

});


//Almacenar en Formulario
// FormUtil.getField(fieldId1).val("total");


// var field = $("#IVA").val();

// To Debug
window.alert(field1);
window.alert(amount);
// console.log("x");
// document.write(5 + 6);

//});

</script>

javascript;forms

4


29 Aug, 2017
confluenceUser
confluenceUser

Based on Number doc, you should use Number.parseFloat() to parse your value from string to number.

03 Sep, 2017
confluenceUser
confluenceUser

Hi, i did use the Number() Object to test and don´t work like we think, the results is the same, apparently the value of this field is NaN or could not be converted to a number.

I did try with this three options :

 var field1 = Number.parseFloat(FormUtil.getField("total_abonado").val());

 var field1 = Number.parseFloat(FormUtil.getField("total_abonado"));

 var field1 = parseFloat(FormUtil.getField("total_abonado").val());

 

I would like to ask if using java script is the best way to take the value of the calculation field in order  to do an aritmetical operation, anybody know if i can use other option to do this?

thanks

03 Sep, 2017
confluenceUser
confluenceUser

You have non-numerical value in your field value thus you are getting NaN error. Try to strip out non-numerical value first. var field1 = Number.parseFloat(FormUtil.getField("total_abonado").val().replace(/[^0-9\.-]+/g,"")); Reference: https://stackoverflow.com/questions/559112/how-to-convert-a-currency-string-to-a-double-with-jquery-or-javascript

09 Sep, 2017
confluenceUser
confluenceUser

Hi Bastiana , thanks for your answer¡

RELATED QUESTIONS

Your answer


To answer a question you'll need an account.

Print