Quantcast
Channel: Dynamics NAVAX
Viewing all articles
Browse latest Browse all 220

str2Num doesn’t work as expected

$
0
0

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.

InputOutput (str2Num)Output (str2NumOk)System.Convert::ToDecimal(strValue);
1 231falsecatch the exception as nothing is resolved
1abc1falsecatch the exception as nothing is resolved
1,123.001false123.00
    
You can use the method str2NumOk() to check

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.

staticvoid 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:


http://msdn.microsoft.com/en-us/library/hf9z3s65.aspx


Viewing all articles
Browse latest Browse all 220

Trending Articles