in my code I need to put a String contained inside a map in a Decimal variable. Now I have 2 problems, the first one is the format of the String because it can contain ' , ' and ' . ' e.g. '21,31' or '3.124,23', the second one is that when I use Decimal.ValueOF(string) method System gives me "invalid decimal error" despite my code. Here my code:

//controll over price format
   }else if((mapFieldColumn.get('UnitPrice')[t].right(4)).contains(',')){
   oli.UnitPrice = Decimal.ValueOf(mapFieldColumn.get('UnitPrice')[t]);

The system debug shows the error: "Exception: System.TypeException: Invalid decimal: € 28836.81 ". The String I was trying to cast is = "2.8836,81"

  • I've removed "cast" from your question as you are using the correct approach of a factory method rather than a cast that would be ((Decimal) stingVariable).
    – Keith C
    Nov 17, 2016 at 10:27
  • thank you keith, convert is absolutely more appropriate. Nov 17, 2016 at 10:34
  • I understand what it's the problem. The String can contain also special char like: € or $ that indicate the current currency. Now I'm working on a way to eliminate this type of characters. Nov 17, 2016 at 10:59

1 Answer 1


Consider using a regular expression and the replaceAll() method.

The following expression will identify any character that is not a decimal point or a digit. You can try it out at regex101.com


In Apex you'll need to escape the slash...


By this you'll be able to strip out the comma without having to go searching for it.

String value = '12,345.678';
system.assertEquals('12345.678', value.replaceAll('[^.\\d]','');
system.assertEquals(12345.678, Decimal.valueOf(value.replaceAll('[^.\\d]',''));

This cleaned-up value should convert to a Decimal without giving an error. You may also find it helpful to create a utility method that takes the String to be converted as well as a Decimal value to return if an error occurs.

  • I take this opportunity to ask you, there is a regex espression that allow me to replace all the currency's simbols. For Example in my org someone use $ someone use €. The same user can use both Dollar or Euro depends on the opportunity Nov 21, 2016 at 9:26
  • The same code provided above will also strip out any currency symbols from a string value, these being neither numeric nor a decimal point. So you could use the same code, then apply some other logic to determine which is the correct symbol to be displayed. If you're working in a multi-currency environment or have a need to convert between USD and EUR, then asking that in a separate question is a better approach then replacing currency symbols. Nov 21, 2016 at 20:59

You must log in to answer this question.

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