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>
...
Best Answer
According to the documentation
currency
field displays a currency using lightning-formatted-number, so you can setminimumFractionDigits
andmaximumFractionDigits
properties to thetypeAttributes
object.I.E.
typeAttributes: { minimumFractionDigits: 0, maximumFractionDigits: 2 }
By the way,
type: 'Currency'
is wrong. It'stype: 'currency'
. Javascript is case sensitive. I updated the code below.