+ Start a Discussion

Amount format issue decimal places 2

setScale(2) doesn't work for me. If the amount is 53, i want 53.00, if it is 53.1, i want it to show as 53.10 but currently it shows as only 53 or 53.1, it cuts off the zero.

Please see my code below;

public class BillAndPayWrapper {
        @AuraEnabled public DateTime postDate;
        @AuraEnabled public String   billPeriod;
        @AuraEnabled public Boolean  showSuccess;
        @AuraEnabled public String      invoice;
        @AuraEnabled public Decimal  amount;
        @AuraEnabled public String      amount_formatted;
        @AuraEnabled public String   docType;
        @AuraEnabled public String      pdfDocURL;
        BillAndPayWrapper(BillingHistoryServiceSet__x billPay, String companyCode) {
            this.postDate      = billPay.PostingDate__c;
            this.invoice      = billPay.InvoiceNo__c;
            this.amount      = billPay.TotalDueAmt__c.setScale(2);
            this.billPeriod  = billPay.BillPeriod__c;
            this.billPeriod = this.billPeriod.right(2) + '/' + this.billPeriod.left(4);

            if (amount<0) {
                this.amount_formatted = '($' + String.valueOf(amount.format()) + ')';
            else  this.amount_formatted = '$' + String.valueOf(amount.format());

please note: external object amount field data type is number
Jessica RiffeJessica Riffe
Are you displaying the data in an aura component? 
If so, why don't you use the <lightning:formattedNumber> tag to display the currency correctly? Reference (https://developer.salesforce.com/docs/component-library/bundle/lightning:formattedNumber/example#lightningcomponentdemo:exampleCurrencyFormatting)
<lightning:formattedNumber value="12.34" style="currency" currencyCode="USD"/>
Should i have the amount as currency in aura component? Currently we are formatting it to decimal and trying to display in the lightning component
Jessica RiffeJessica Riffe
I suppose it depends on your requirement.  If you are using an aura component, then the option in my comment above will format the number into currency format for you.  If it is a lightning web component, you can use this: 
<lightning-formatted-number value="12.34" format-style="currency" currency-code="USD"></lightning-formatted-number>

Documentation link (https://developer.salesforce.com/docs/component-library/bundle/lightning-formatted-number/example):

Now.. as for how should you display it.. Even if you want to display it just as a decimal number but no currency sign, you can still use the formattednumber tag.  The documentaiton in the links should show you how to do it.   You should also be able to display the negative ($0.00) on the client side with an aura:if (if using aura components) or <template if:True (for LWC).

For example:
<aura:if isTrue="{!wrapper.amount > 0}">
       <lightning:formattedNumber value="{!wrapper.amount}" style="currency" currencyCode="USD" />

        <aura:set attribute="else">
            <lightning:formattedNumber value="{!wrapper.amount}" style="currency" currencyCode="USD" />
thanh Tâmthanh Tâm
I think it depends on your requirement. If you use the aura component, the option in my comment above will format the number in currency format for you. If this is a lightning web component, you can use this:
GGmedia cung cấp dịch vụ SEO (https://ggmedia.biz/
Deepali KulshresthaDeepali Kulshrestha
Hi Meena,

Use this code to create an input field that will show double decimal places.
<lightning:input type="currency" min="0" step="0.01"/>

'step' is used to define how many decimal places you want to show.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha