The built-in function Fn::Calculate returns the calculation result of an expression of the String type.
Declaration
JSON
{ "Fn::Calculate": [ "expression", ndigits, [ <number1>, <number2>, ... ] ] }YAML
Syntax for the full function name:
Fn::Calculate: - expression - ndigits - - <number1> - <number2> - ...Syntax for the short form:
!Calculate - expression - ndigits - - <number1> - <number2> - ...
Parameters
expression: The parameter specifies an expression of the String type.ndigits: The parameter specifies the number of decimal places to display. This parameter value must be 0 or a positive integer. The parameter takes effect only when the expression contains floating-point numbers.[<number0>, <number1>, <number2>, ... ]: Optional. You can define {n} in the expression. n specifies the index of a number. When the expression is being calculated, {n} is replaced with the number.
Return value
The calculation result of the expression. The return value is of the Number type.
Examples
!Calculate
- (2+3)/2*3-1
- 1
!Calculate
- (2.0+3)/2*3-1
- 1
!Calculate
- ({1}+3)/2*3-1
- 1
- - 3
- 5
- 6
!Calculate
- ({0}+{1})%3
- 0
- - 5
- 6In this example, the following result is returned:
5
6.5
11
2Decimal places are not retained for the quotient of integers. For example, 5/2 = 2. Therefore, the return value of Fn::Calculate:(2+3)/2*3-1, 1 is 5.