If you are trying to convert a string to a decimal, you might notice that it doesn’t always work. It is very sensitive on the formatting of the string.
Input | Output (str2Num) | Output (str2NumOk) | System.Convert::ToDecimal(strValue); |
1 23 | 1 | false | catch the exception as nothing is resolved |
1abc | 1 | false | catch the exception as nothing is resolved |
1,123.00 | 1 | false | 123.00 |
Alternatively, you could use the .NET system method to convert the string to a decimal. This handles conversions a little better.
real decimalValue = System.Convert::ToDecimal(strValue);
Sample job code to test it out the third example.
static void testStringToNumeric(Ar gs _args)
{
str strValue;
real realValue; strValue = "1,123.00";
print str2num(strValue); //returns 1
print str2NumOk(strValue); //returns false
try
{
realValue = System.Convert::ToDecimal(strValue);
print realValue;
}
catch
{
print "caught";
} pause; }
Reference: