Format a CURRENCY type field in a datatable in LWC

apexjavascriptlightning-web-componentslwc-domsalesforcedx

How to format a CURRENCY type field (named "Amount") in a datatable so that there are only 2 digits at the end of the number after the dot (in the fractional part)? The table displays an object received from salesforce via apex in lwc.

description of columns in js file:

...
const COLUMNS_FOR_ACC = [
    {
        label: 'View',
        type: 'button-icon',
        initialWidth: 75,
        typeAttributes: {
            iconName: 'action:preview',
            title: 'Detail',
            variant: 'border-filled',
            alternativeText: 'View'
        }
    },
    { label: 'Name', fieldName: 'Name', wrapText: true, hideDefaultActions: true },

    //Problem field below
    { label: 'Amount', fieldName: 'Amount', type: 'Currency', wrapText: true, hideDefaultActions: true },
    { label: 'CreatedDate', fieldName: 'CreatedDate', type: 'date', wrapText: true, hideDefaultActions: true, typeAttributes:{day:'numeric',month:'short',year:'numeric',
    hour:'2-digit',minute:'2-digit',second:'2-digit',hour12:true}},
    { label: 'CloseDate', fieldName: 'CloseDate', type: 'date', wrapText: true, hideDefaultActions: true }
];

...

export default class AccordionEx extends LightningElement
{
    columnsForAcc=COLUMNS_FOR_ACC;
    
...

using datatable in html file:

...
<lightning-accordion allow-multiple-sections-open={multiple}>
        <template if:true={accountsOnThisPage}>
            <template for:each={accountsOnThisPage} for:item="acc">
                <lightning-accordion-section name={acc.Name} label={acc.ServiceText__c} key={acc.Id}>
                    <!-- The table is here -->
                    <lightning-datatable key-field="Id" data={acc.Opportunities} columns={columnsForAcc} onrowaction={handleRowAction} hide-checkbox-column="true" ></lightning-datatable>
                </lightning-accordion-section>
            </template>
        </template>
    </lightning-accordion>
...

This is what I get as a result:
enter image description here

Best Answer

According to the documentation currency field displays a currency using lightning-formatted-number, so you can set minimumFractionDigits and maximumFractionDigits properties to the typeAttributes object.
I.E. typeAttributes: { minimumFractionDigits: 0, maximumFractionDigits: 2 }

By the way, type: 'Currency' is wrong. It's type: 'currency'. Javascript is case sensitive. I updated the code below.

const COLUMNS_FOR_ACC = [
    {
        label: 'View',
        type: 'button-icon',
        initialWidth: 75,
        typeAttributes: {
            iconName: 'action:preview',
            title: 'Detail',
            variant: 'border-filled',
            alternativeText: 'View'
        }
    },
    { label: 'Name', fieldName: 'Name', wrapText: true, hideDefaultActions: true },
    { label: 'Amount', fieldName: 'Amount', type: 'currency', wrapText: true, hideDefaultActions: true, typeAttributes: { minimumFractionDigits: 0, maximumFractionDigits: 2 } },
    { label: 'CreatedDate', fieldName: 'CreatedDate', type: 'date', wrapText: true, hideDefaultActions: true, typeAttributes:{day:'numeric',month:'short',year:'numeric',
    hour:'2-digit',minute:'2-digit',second:'2-digit',hour12:true}},
    { label: 'CloseDate', fieldName: 'CloseDate', type: 'date', wrapText: true, hideDefaultActions: true }
];
Related Topic