Rounding Values in Variable Expressions
Rounding values in variable expressions
As the expression is evaluated as a JavaScript program (see the previous section) we can use some of the core functions in JavaScript to alter the variable value. For example, in our example of calculating a variable STRESS from an expression %FORCE%/%AREA% this could have a large number of significant figures in the result.
E.g. if FORCE=10 and AREA=3 then stress is 3.33333333333333 which is far more significant figures than we require.
We can use the core JavaScript function toFixed() to change the number of digits to appear after the decimal point. If we wanted 2 decimal places then we could change the expression to
(%FORCE% / %AREA%).toFixed(2)
which would change the value of STRESS to 3.33.
Other useful functions are:
- toExponential(n) which formats the number in exponential (scientific) notation with n digits after the decimal point.
- toPrecision(n) which formats the number with n significant figures.