cancel
Showing results for 
Search instead for 
Did you mean: 
iburiak
Sisense Team Member
Sisense Team Member

How to represent negative numbers with leading and trailing parentheses

In Accounting, it is common to represent negative numbers with leading and trailing parentheses.

For example, negative two hundred is displayed as "(200)"

Also, It incorporates a thousand comma separator. In the event that decimal values are absent, it appends ",00"; if present, it retains them. Furthermore, if there is only one decimal place after the comma, an additional zero is added for a more aesthetically pleasing format.

 

widget.transformPivot({ type: ['value'] }, function(metadata, cell) {

    if (cell.value >= 0 || !cell.value) {

        cell.content = '$' + formatNumber(cell.value);

        cell.contentType = 'html';

    } else {

        cell.content = '$(' + formatNumber(-1 * cell.value) + ')';

        cell.contentType = 'html';

    }

});




function formatNumber(value) {

    if (value === null || value === undefined) {

        return '';

    }




    const formattedValue = value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');




    if (formattedValue.includes('.')) {

        const decimalPart = formattedValue.split('.')[1];

        if (decimalPart.length === 1) {

            return formattedValue + '0';

        } else {

            return formattedValue;

        }

    } else {

        return formattedValue + '.00';

    }

}


Before:

iburiak_0-1704407351489.png

After:

iburiak_1-1704407351595.png
Leave a comment if this was helpful! 

Disclaimer: This blog post contains one possible custom workaround solution for users with similar use cases. We cannot guarantee that the custom code solution described in this post will work in every scenario or with every Sisense software version. As such, we strongly advise users to test solutions in their environment before deploying them to ensure that the solutions proffered function as desired. To avoid doubt, the content of this blog post is provided to you "as-is" and without warranty of any kind, express, implied, or otherwise, including without limitation any warranty of security and or fitness for a particular purpose. The workaround solution described in this post incorporates custom coding, which is outside the Sisense product development environment and is, therefore, not covered by Sisense warranty and support services.

 
Rate this article:
Version history
Last update:
‎01-15-2024 07:35 AM
Updated by:
Contributors