Calculations with formatted fields

Calculations with formatted fields

martinconnollybartmartinconnollybart Posts: 20Questions: 8Answers: 0

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

  • rf1234rf1234 Posts: 2,988Questions: 87Answers: 421
    edited November 1 Answer ✓

    Is there something obvious that I am missing here?

    I guess so, Martin :wink:

    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

    function toFloat(fn, spaceIfNaN) {
        if ( typeof spaceIfNaN === "undefined" ) {
            spaceIfNaN = false;
        }
        if (lang == 'de') {
            fn = fn.toString().replace( /[\.]/g, "" );
            fn = fn.toString().replace( /[\,]/g, "." );            
        } else  {
            fn = fn.toString().replace( /[\,]/g, "" );
        }
        fn = parseFloat(fn);
        if ( ! isNaN(fn) ) {
            return fn;
        }
        if ( spaceIfNaN ) {
            return "";
        }
        return 0;
    }
    
    function toFormattedNum(ufn) {
        var numberRenderer;
        if (lang == 'de') {
            numberRenderer = $.fn.dataTable.render.number( '.', ',', 2 ).display;
        } else {
            numberRenderer = $.fn.dataTable.render.number( ',', '.', 2 ).display;
        }
        return numberRenderer(ufn);
    }
    

    You would need something like this:

    this.set( { 'TApplications.totalrequested':
                toFormattedNum(
                toFloat(this.val('TApplications.reqy1')) +
                toFloat(this.val('TApplications.reqy2')) +
                toFloat(this.val('TApplications.reqy3')) +
                toFloat(this.val('TApplications.reqy4')) +
                toFloat(this.val('TApplications.reqy5'))   ) } );    
    

    Roland

  • martinconnollybartmartinconnollybart Posts: 20Questions: 8Answers: 0

    Thank you, using this example I got the code working correctly!

Sign In or Register to comment.