• #19863
    Salesforce | Manpreet Manpreet #229
    Forcetalks

    What is a concise function that formats a (String) decimal into a currency format in Salesforce Apex?

    What is a concise function that formats a (String) decimal into a currency format in Apex?

     

    #19877
    Salesforce | Saurabh Saurabh #132
    Forcetalks

    Hi Manpreet

    public static String currency(String i) {
    String s = ( Decimal.valueOf(i==null||i.trim()==”?’0′:i).setScale(2) + 0.001 ).format();
    return s.substring(0,s.length()-1);
    }

    Hope it may help

    #39590
    Salesforce | Parul Parul #2
    Forcetalks

    Currency format of different countries are different,so we assume here “1,000.00” as currency format because in most of English countries this currency format is used.Let’s try this code-

    String i=’2050066.50′;
    String s = ( Decimal.valueOf(i==null||i.trim()==”?’0′:i).setScale(2) + 0.001 ).format();
    String p = s.substring(0,s.length()-1);
    System.debug(p);

     

    Thanks

    #41460
    Forcetalks

    The following should do the trick, with the assumption that your locale for currency formatting is correct. Edited to handle null being passed, and zero values after the decimal.

    public static String currency(String input) {
    if ( input == null ) {
    return '0.00';
    }

    Decimal d1 = Decimal.valueOf(input).setScale(2);
    String str = d1.format();
    if( !str.contains('.' ) ) {
    str = str + '.00';
    }

    return str;
    }

    Thanks.

Viewing 4 posts - 1 through 4 (of 4 total)

Please to reply to this topic.

CONTACT US

We're not around right now. But you can send us an email and we'll get back to you, asap.

Sending

About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Copyright 2019 Forcetalks. All Right Reserved.

Log in with your credentials

or    

Forgot your details?