Calculations with formatted fields
Calculations with formatted fields
I have a number of fields in my editor that are formatted in the php file as follows:
Field::inst( 'TApplications.reqy1' )
->getFormatter( function ($val){ return $val == null? '' : number_format($val,2,'.','');}),
Field::inst('TApplications.reqy2')
->getFormatter( function ($val){ return $val == null? '' : number_format($val,2,'.','');}),
and so on up to reqy5.
So each field ends up as '3000.57' for example.
Then I have a read-only field ('totalrequested') that should add these fields together using the dependent() API:
appEditor.dependent(
['TApplications.reqy1', 'TApplications.reqy2', 'TApplications.reqy3','TApplications.reqy4','TApplications.reqy5'],
function (val, data, callback) {
appEditor.set( { 'TApplications.totalrequested':
appEditor.val('TApplications.reqy1') +
appEditor.val('TApplications.reqy2') +
appEditor.val('TApplications.reqy3') +
appEditor.val('TApplications.reqy4') +
appEditor.val('TApplications.reqy5') } );
return {};
}
);
But the field value is just a concatenation of the formatted numbers. I tried it without the formatting and the same happens.
I also tried converting the values of the 5 fields to numbers by wrapping them in 'Number()' but then the calculated field shows as 'NaN'.
Is there something obvious that I am missing here?
This question has an accepted answers - jump to answer
Answers
I guess so, Martin
I have a similar situation. I return two different types of formatted numbers depending on the user language: English (UK) and German (Germany). I guess number formatting is the same in the UK and the US, only date formatting is different - if I recall this correctly.
I made two little helper functions to create float numbers and to create formatted numbers again after calculating. "lang == 'de'" is the German formatting "else" is English.
German formatted number, e.g. 1.000.000,00
English formatted number, e.g. 1,000,000.00
You would need something like this:
Roland
Thank you, using this example I got the code working correctly!