function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Priyanka Reddy 74Priyanka Reddy 74 

How to convert List <String> to List of decimals

Hi All,

I have a List<String> values and i want to convert it into List<Double> or decimal.
 
string s1 = '100,22,5'; 

List<String> splitDoubleValues = s1.split(',');
List<Double> doubleList = new List<Double>();
            for(Integer i=0;i<splitDoubleValues.size();i++){
              doubleList.add(decimal.valueOf(splitDoubleValues[i]));
                system.debug('doubleList :'+doubleList);
                //selectedList3.put(doubleFeilds[i],doubleList[i]);		
            }


Can some one suggest how to convert it.

Thank you.
RituSharmaRituSharma
Your current code will work. Instead of decimal.valueOf method, you may use double.valueOf method.
ANUTEJANUTEJ (Salesforce Developers) 
Hi Priyanka,

I was able to use the below code and I was able to print the decimal values you can try checking this.
 
string s1 = '100,22,5'; 

List<String> splitDoubleValues = s1.split(',');
system.debug(splitDoubleValues);
for(String s: splitDoubleValues)
{
// to get only the integer value​​​​

system.debug(Decimal.valueOf(s)
//to get the floating value
system.debug(double.valueof())
);}
In the above snippet, I used for each loop instead of for loop

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.