You need to sign in to do that
Don't have an account?
MMA_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
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
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.
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
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
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
}
}
Would you or anyone have a method to do the same in VF.. instead of a method in APEX class??
Thanks
Vote for my suggestion on this:
Format Number/Currency
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.
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.
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.