[SalesForce] Function to convert String to Decimal value

I need to convert String into Decimal. the isNumeric() method is retuning false if value is Decimal. Another alternate:

try
{
    Integer x = Integer.valueOf(myString);
}
catch (exception e)
{
    // it's not an Integer, try something else
}

Is there any other way to convert Decimal into String?

Note: value which is coming can be Integer or String.

Best Answer

IsNumeric() method works for only Integer value not for decimal value

like this

String str = '10';
system.debug('===strToDec=='+str.isNumeric());

This will return true.


String str = '10.25';

Decimal strToDec = decimal.valueOf(str);

system.debug('===strToDec=='+strToDec);

String decToStr = String.valueOf(strToDec);

system.debug('===decToStr=='+decToStr);

Run this code in developer console ad check output.


In you case do something like. First convert string to numeric then check and again convert string into decimal for future use

String  str = '10.25';
Integer intCheck = Integer.ValueOf(str);
Decimal decVal = Decimal.ValueOf(str);
if(String.ValueOf(intCheck ).isNumeric())
//use decVal here

Don't understand why you need this.