While working as a Salesforce developer, I recently fixed an issue related to the input product pricing field data type. In the Salesforce application, there was a lightning component where the user could enter the product price, and by default, the input field was a string.
To perform calculations with the entered value, such as applying taxes or discounts, the field data must be in decimal format.
In this Salesforce tutorial, I will explain how to convert a string to a decimal in Salesforce Apex.
Convert a String to Decimal in Salesforce Apex
To convert a string to a decimal object in Salesforce Apex, navigate to the Salesforce developer console and follow the steps below.
- In the Salesforce developer console, first create an Apex class. Select File > New > Apex Class.
- Enter the name of a new apex class and click the OK button.
- Enter the code below in the developer console and save it.
public class ConvertStringtoDecimal {
public static Decimal convertStringToDecimal(String strNumber) {
return Decimal.valueOf(strNumber);
}
}In the above apex public class, we have used the Decimal.valueOf(strNumber) method, which converts the input string strNumber into a Decimal value using the Decimal.valueOf() method.
- After saving the apex code, if there are no errors, press “Ctrl+E” to execute the code.
- In the anonymous window, enter the code below, select “Open in Log,” and press the Execute button.
String strNumber = '456.80';
Decimal decimalNumber = ConvertStringtoDecimal.convertStringToDecimal(strNumber);
System.debug('Converted Decimal: ' + decimalNumber);
- In the console log, select the option Debug only. Now, you will see the string formatted in a decimal data type.

Following the above steps, we can convert string data to a decimal in Salesforce Apex.
Convert List of Strings to List of Decimals in Salesforce Apex
- In the Salesforce developer console, first create an Apex class. Select File > New > Apex Class.
- Enter the name of a new apex class and click the OK button.
- Enter the code below in the developer console and save it.
public class ListofStringtoDecimal {
public static void convertList() {
List<String> stringList = new List<String>{'1.23','4.56','7.89','10.11'};
List<Decimal> decimalList = new List<Decimal>();
for (String str : stringList) {
decimalList.add(Decimal.valueOf(str));
}
System.debug('Decimal List: ' + decimalList);
}
}- After entering the code, check for errors. If the Problems section does not display any error, then execute the code.
- In the anonymous window, enter “ClassName.MethodName();” then select the checkbox “Open in log” and click the Execute button.

- In the execution log window, select the checkbox to debug only.
Then, in the debug details, you will see the decimals converted from the list of strings in Salesforce Apex using the Decimal.valueOf(str) funtion.

Following the above steps, you can convert a list of strings to a list of decimals in Salesforce Apex.
Convert a String to Decimal in Apex Using Specific Locale
This situation typically arises when dealing with currency field data, such as prices formatted differently depending on the locale. In this case, there aren’t any Salesforce-provided methods of doing this.
Below is an example of three countries with different decimal formats in their locale.
CH : SFr. 41'700.20
FR : 41 700,20 €
UK : £ 41,700.20But we can achieve it by normalizing the input, and I will explain in the steps below.
- We will solve it with the class method, so to create a class in the Apex developer console, select File > New > Class.
- Enter the class name and click on the OK button.
- Enter the code below in the developer console and save it.
public class StringToDecimalLocale {
public static void normalizeAmounts() {
List<String> inputAmountStrings = new List<String>{'SFr. 50\'500.20', '65 700,20 €', '£ 30,550.10'};
List<String> firstNormalization = new List<String>();
List<String> secondNormalization = new List<String>();
List<Decimal> decimalAmounts = new List<Decimal>();
for (String input : inputAmountStrings) {
firstNormalization.add(input.replaceAll('[^0-9]', ' ').trim());
}
for (String input : firstNormalization) {
String decimals = input.substringAfterLast(' ');
if (decimals.length() == 2) {
secondNormalization.add(input.substringBeforeLast(' ').replace(' ', '') + '.' + decimals);
} else {
secondNormalization.add(input.replace(' ', ''));
}
}
for (String normalizedAmount : secondNormalization) {
Decimal amount = Decimal.valueOf(normalizedAmount);
decimalAmounts.add(amount);
System.debug('Decimal Amount: ' + amount);
}
}
}Let us understand how we converted the locale format of the string decimal value to convert it to decimals in the above code.
- In the first normalization, we have trimmed special characters like currency signs and non-decimal characters with whitespace. After the first normalization, the values will become ‘50 500 20′, ’65 700 20′, and ’30 550 10′.
- The second normalization step determines whether there are any decimal places and gives us the final results, which you can convert to a Decimal type.
- After the second normalization, the decimal values are consistent but still in string format. To convert them to decimal, we have used the Decimal.valueOf() function for the value of “normalizedAmount.”
- If you do not have any errors in the above code, execute it using the shortcut keys “Ctrl+E.“
- Enter the code below in the anonymous window, select the checkbox Open Log, and then click on the Execute button.

- In the execution log window, select the checkbox Debug only. Then, you will see the decimal values converted from a string.

This way, we can convert a string to a decimal in Apex using a specific locale with the help of the above code.
Conclusion
In this Salesforce tutorial, we have learned to convert a string to a decimal for a single value. Then, we converted a list of strings to a list of decimals in Salesforce Apex. After this, we converted the string to decimal currency values from their locale format.
By now, you might have understood the string-to-decimal conversion in Salesforce Apex and using Decimal.valueOf(); function in it.
You may also like to read
- Convert String to Boolean in Salesforce Apex
- Convert String to ID in Salesforce Apex
- Convert String to Date in Salesforce Apex
- Convert a String to an Integer using Apex in Salesforce

Abhijeet is a skilled Salesforce developer with experience in developing and integrating dashboards, data reports, and Salesforce applications. He is also skilled at optimizing processes and flow automation processes, coding, and executing complex project architecture. Read more about us | LinkedIn Profile.