a (operator) b
The actual rules are relatively simple:
1) If both a and b are integers, the result is an integer.
2) For , *, **, /, and \: Strings are evaluated as an integer or float, as
appropriate.
3) For +: If the first operand is a string, then string concatenation is
indicated; otherwise Rule 2 applies.
The practical result is that if you want to force a string to be a number,
prefix it with "0+":
$print "3.5" + "4.0"
3.54.0
$print 0 + "3.5" + "4.0"
7.5
If you want to force a string, prefix the expression with "":
$print 3.5 + 4.0
7.5
$print "" + 3.5 + 4.0
3.54..0
If you want to coerce a float to an integer, use \ (like Math.floor in
JavaScript) or %0 (evenvalue rounding, like Math.round)
$print 13.5 \ 3
4
$print (13.5 / 3) %0 + 1
5
Note that %n with n > 0 is used with a float to generate a string with a
given precision:
$print 13.5%2 + " A"
13.50 A
and %n with n < 0 is used to go into scientific notation as a string:
$print 13.5%2 + " A"
1.4E+1 A
