In apex, how would you set the value of a currency field with a formatted string? See the sample code:

String price = '$1,000.00';

MyObj__c obj = new MyObj__c();

// this line won't compile, so I've commented it out:
// obj.price__c = price;

// this line would generate a runtime error: 'Invalid Decimal'
obj.price__c = Decimal.valueOf(price);

However, on a standard layout, you can use dollar signs, commas, decimals, etc. with no problem. One approach is to strip the dollar signs and commas. This would work for US currency but not for all currencies, which would apply in multi currency orgs. Would you have to write a function to look at the user's currency setting and strip the invalid characters?

By the way, the string value is getting passed in via javascript, so it is not done through visualforce databinding.

  • did you look at salesforce.stackexchange.com/questions/318/… ?
    – cropredy
    Commented Jul 31, 2013 at 22:18
  • I did look at that and the answer by @kibitzer mentions writing the code to handle currency symbols and multi currency. I was hoping there was another solution. Thanks though.
    – rycornell
    Commented Jul 31, 2013 at 23:03

1 Answer 1


I did something like below : \p{sc} is the REGEX that matches all the currency

string i= '₪10,000';
Pattern dollarPattern = Pattern.compile('[\\p{Sc},]');
string s = dollarPattern.matcher(i).replaceAll('');
decimal d = decimal.valueof(s);

I tried this with £,¥,₪ and $ and got 10000 as consistent output.

I looked at this post and got an idea on the Regex for currency : https://stackoverflow.com/questions/14169820/regular-expression-to-match-all-currency-symbols

Using pattern matcher I replaced all with ''.

To test with multiple currencies use http://currencies.typeit.org/ if you did not know how to type the currencies like me.


Do you think this works ?

string i= '₪100,000,003';
    i= i.replaceall(',','');}
Pattern dollarPattern = Pattern.compile('[\\p{Sc}]');
string s = dollarPattern.matcher(i).replaceAll('');
decimal d = decimal.valueof(s);

Hope this helps.

  • 1
    This is a good start, but what about currency formats where commas are used to represent decimal places and vice versa? In that case you would want modify the regex to include periods instead of commas. The function would need to be more dynamic.
    – rycornell
    Commented Aug 1, 2013 at 0:26
  • 1
    Your update would not work for multi currencies. Check out Salesforce's supported locales page. You would need to be able to support different number formats e.g. $1,234.45 and 1.234,56 and 1'234.56
    – rycornell
    Commented Aug 1, 2013 at 13:04
  • 1
    Yep you are right. Exploring java and javascript forums there is no specific regex that can address all the currency formats. I will look for some jquery plugins (if any?) that can do the trick, meanwhile add this question to bounty if you are interested. I am curious to know how multi currency management can be implemented. Finally is there any reason why you are not sticking on to visualforce data binding and choosing javascript route, are you using Jquery selectors, templates that do not go hand in hand with Visualforce?
    – Rao
    Commented Aug 1, 2013 at 16:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .