Modeller.convertFromString


convertFromString(expr, output, [showErrors], [error], [locale], [variablesIn], [variablesNeeded], [dimensionality], [unitSet])

Attempts to convert the given string into a numerical value. Any expressions in the string (e.g. "3+4") are evaluated.

exprstringThe input expression
outputdouble The output numeric value (if the function succeeds)
showErrorsoptional boolean If true, LUSAS Modeller will generate pop-up dialogs to inform the user why the string could not be evaluated into a number
erroroptional string If given, this string will be filled in with the reason why the string could not be evaluated into a number
localeoptional string If given, the locale in which to make the conversion, examples are similar to those shown in the Windows international settings dialog: "English", "English_United Kingdom", "French", "French_Monaco" etc. The special value of "C" may be used to represent standard US English number formating If not given, the Windows locale at the time Modeller was launched will be used
variablesInoptional 2d array If given, an array of variables and values that may appear in the expression. For example, if the array contains just one pair of items, the string "x" and the value 4, then x may be used in the expression, and will be assumed to have the value 4. If the array has two pairs of items "x", 3 and "y","x*2", x and y may both be used in the expression. x will have the value 3 and y the value 6.
variablesNeededoptional array of strings If the return code is 2, this argument will be set to be array of the names of the variables that were needed
dimensionalityoptional integer Defines the dimensionality of the input string.
unitSetoptional string The unit set that we need to convert to (e.g "N,mm,t,s,C"). If it is not given (unitSet = ""), no conversion will occur.
Return valueintegerZero if the string could be evaluated to a number. 2 if the string depends on variables that were not given. Other non-zero values are errors.

Example


 if (convertFromString("3+4", seven)) then
     call msgbox("oops, that shouldn't happen")
 else
     call msgbox("3+4=" & seven)
 endif
 if (convertFromString("3/0", infinity)) then
     call msgbox("Modeller should have just reported divide by zero")
 else
     call msgbox("oops, that shouldn't happen")
 endif
 if (convertFromString("log(0)", infinity, false)) then
     call msgbox("Modeller should not have just reported any error")
 else
     call msgbox("oops, that shouldn't happen")
 endif
 if (convertFromString("5+hello", missingVar, false, myErrStr)) then
     call msgbox("The next message box should say Unknown variable hello")
     call msgbox(myErrStr)
 else
     call msgbox("oops, that shouldn't happen")
 endif
 ' convert a stress of 5.0 (given in model units) into "N,mm,t,s,C"
 convertFromString("5.0", converted, false, myErrStr, "C", nothing, nothing, packDimensionality(0,1,-2,0,0,0), "N,mm,t,s,C")
 

See also convertToString

Back to Modeller

Back to Overview