Automatic conversion to a number
Describes the automatic conversion of numbers in OPL functions.
When a function or a method which expects a number as one of its arguments is passed a nonnumeric value, it tries to convert this value to a number using the following rules:
A string is parsed as a number literal. If the string does not represent a valid number literal, the conversion yields NaN.
The Boolean
trueyields the number 1.The Boolean
falseyields the number 0.The null value yields the number 0.
A date yields the corresponding number of milliseconds since 00:00:00 UTC, January 1, 1970.
For example, if the Math.sqrt function
is passed a string, this string is converted to the number it represents:
Math.sqrt("25") 5
Similarly, operators which take numeric operands attempt to convert any nonnumeric operands to a number:
"3" * "4" 12
For operators that can take both strings (concatenation)
and numbers (addition), such as +, the
conversion to a string takes precedence over the conversion to a number
(See Automatic conversion to a string.). In
other words, if at least one of the operands is a string, the other
operand is converted to a string; if none of the operands is a string,
the operands are both converted to numbers. For example:
"3" + true "3true"
3 + true 4
For comparison operators, such as == and >=,
the conversion to a number takes precedence over the conversion to
a string. In other words, if at least one of the operands is a number,
the other operand is converted to a number. If both operands are strings,
the comparison is made on strings. For example:
"10" > "2" false
"10" > 2 true