function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MMA_FORCEMMA_FORCE 

Format Number??

Hi all:

  Does anyone know how to format a number in Apex so that a number in VF appears correctly...

for example 25000 appears 25,000.00

Anyone have an idea or did this already??

Thanks

Let MMA guide you

Best Answer chosen by Admin (Salesforce Developers) 
Michael_KahleMichael_Kahle

Hi MMA_Force,

 

i created a Method to format exactly what you wanted.

 

 

public static String doFormatting(Decimal val, String osep, String nsep) { String s, tmp; Integer i = 6; s = val.setScale(2).toPlainString().replace(osep, nsep); while(s.length() > i) { tmp = s.substring(0, s.length() - i) + osep + s.substring(s.length() - i); s = tmp; i += 4; } return s; }

 

It takes an Decimal Value and to separators thousand seperator and decimal separator and creates a String. 

 

Decimal d = 25000.0; System.debug('# ' + doFormatting(d,'.',',')); // # 25.000,00

 

 

 

All Answers

ShikibuShikibu

If the Decimal is associated with a Currency field salesforce will correctly format it as currency.

 

If you do system.debug( Decimal.valueOf( '25000' ).setScale(0).format() ); you get "25,000".

 

There's not a simple way to control how many decimal digits are displayed by format(). You may have to mung the string yourself. 

 

Michael_KahleMichael_Kahle

Hi MMA_Force,

 

i created a Method to format exactly what you wanted.

 

 

public static String doFormatting(Decimal val, String osep, String nsep) { String s, tmp; Integer i = 6; s = val.setScale(2).toPlainString().replace(osep, nsep); while(s.length() > i) { tmp = s.substring(0, s.length() - i) + osep + s.substring(s.length() - i); s = tmp; i += 4; } return s; }

 

It takes an Decimal Value and to separators thousand seperator and decimal separator and creates a String. 

 

Decimal d = 25000.0; System.debug('# ' + doFormatting(d,'.',',')); // # 25.000,00

 

 

 

This was selected as the best answer
MMA_FORCEMMA_FORCE

Hi Thanks... I need to use it in this query but am getting an error...

 

quote = [Select s.Opportunity__r.Pricebook2Id, Quote_Amount_rollup__c,LastmodifiedDate, (Select Unit_Price__c, Unit_Net_Price__c, ServiceDate__c, Sales_Discount__c, Quote__c, Qty_Ordered__c, Product2__c, Product2__r.Name, Name, Id, Ext_Price__c, Ext_Net_Price__c, Ext_Price_tmp__c, Description__c From Quote_Lines__r order by name ), s.Opportunity__r.HasOpportunityLineItem, s.Opportunity__r.Name, s.Name,s.Partner_Direct__c, s.Opportunity__r.Id, s.Opportunity__c From SFDC_520_Quote__c s where s.id = :id limit 1]; a4a = doFormatting(quote.Quote_Lines__r.Unit_Price__c,'.',',');

 The error I get is Compile Error: Invalid foreign key relationship: SFDC_520_Quote__c.Quote_Lines__r at line 38 column 32... Anyone know how to pick out the field I need?

Thanks

 

 

ShikibuShikibu

The forum software doesn't show line numbers, and when I cut and paste to an editor there are no line breaks in your pasted code, so I can't be sure where your compile error is happening.

 

But it looks like you have "From Quote_Lines__r" in the select as well as in doformatting.

 

 

Quote_Lines__c should be the name of your custom object (which you can use after "from" in select), and Quote_Lines__r is the name of the relationship (which you can use before "." to access fields in the related record). The name of the relationship is set along with the foreign key field as "child relationship name".

 

If you want to view all the relationship names, you can use Workbench, or getDescribe, or examine the wsdl.

 

Also, when you do a subselect, the subselect is a list, so you need nested for statements. This page of the apex guide shows how to access the inner select's data:

 

 

for (Account a : [SELECT id, name, (SELECT lastname FROM contacts)
FROM account
WHERE name = 'Acme']) {
for (Contact c : a.contacts) {

// do something with this contact for this account

}
}

 

 

 

 

 

Message Edited by Shikibu on 04-25-2009 07:45 AM
MMA_FORCEMMA_FORCE

Would you or anyone have a method to do the same in VF.. instead of a method in APEX class??

Thanks

 

MMA_FORCEMMA_FORCE

Vote for my suggestion on this:

Format Number/Currency

Michael_KahleMichael_Kahle

Hi,

 

Visualforce is a Description Language, not a programming language. Therefore there is no way to do it Native in Visualforce or HTML. But you can try and use Javascript for this.

I do not have a complete method for that, but i think it can easily be done with simple String functions. You just need to use a Mergefield to get the Value into the Javascript on the site.

blakeLLblakeLL

I added this doFormatting method to my custom controller. I have my test working so all is good there.

 

However, how do I call it from a visualforce page?

 

When I use doFormatting(variable); it says doFormatting doesn't exist.

 

Please help.

ShikibuShikibu

VF is a model-view-controller framework. VF pages are part of the "view," so they cannot "call" code in the controller. Rather, a page is associated with an instance of the controller class. In the controller, prepare the data the view (page) needs and expose it with a public getter (or as a public property).

  

I suggest that you read over some introductory material:

 

http://wiki.developerforce.com/index.php/An_Introduction_to_Visualforce 

 

Here's an example; this is a public property that turns a plain text string with newlines into primitive html. If you display {!controller.customMessageHtml} on the page, you get the formatted string. customMessage must be a variable present in the controller instance.

 

 



public String customMessageHtml {
get {
return customMessage.replace('\n', '<br/>');
}
}

 

 

 

narsavagepnarsavagep
Here's the solution I came up with:
private String DisplayDecimal (Decimal inVal, Integer inScale) {
    if ( inVal == null ) return '';
    Decimal outVal = inVal;
    if ( inScale != null ) outVal = inVal.setScale(inScale);
    String rtnStr = outVal.toPlainString();

    // Add commas (if applicable)
    Integer i = rtnStr.indexOf('.');
    if ( i == -1 ) i = rtnStr.length();
    i = (rtnStr.length() - i) + 3;
    while(rtnStr.length() > i) { 
        rtnStr = rtnStr.substring(0, rtnStr.length() - i) + ',' + rtnStr.substring(rtnStr.length() - i); 
        i += 4; 
    }
    
    return rtnStr;
}