ARM 模板的数值函数Numeric functions for ARM templates

资源管理器提供了以下可用于在 Azure 资源管理器 (ARM) 模板中处理整数的函数:Resource Manager provides the following functions for working with integers in your Azure Resource Manager (ARM) template:

addadd

add(operand1, operand2)

返回提供的两个整数的总和。Returns the sum of the two provided integers.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
operand1operand1 Yes intint 被加数。First number to add.
operand2operand2 Yes intint 加数。Second number to add.

返回值Return value

一个整数,包含参数的总和。An integer that contains the sum of the parameters.

示例Example

以下示例模板将添加两个参数。The following example template adds two parameters.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "first": {
            "type": "int",
            "defaultValue": 5,
            "metadata": {
                "description": "First integer to add"
            }
        },
        "second": {
            "type": "int",
            "defaultValue": 3,
            "metadata": {
                "description": "Second integer to add"
            }
        }
    },
    "resources": [
    ],
    "outputs": {
        "addResult": {
            "type": "int",
            "value": "[add(parameters('first'), parameters('second'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
addResultaddResult intInt 88

copyIndexcopyIndex

copyIndex(loopName, offset)

返回迭代循环的索引。Returns the index of an iteration loop.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
loopNameloopName No stringstring 用于获取迭代的循环的名称。The name of the loop for getting the iteration.
offsetoffset No intint 要添加到的从零开始的迭代值的数字。The number to add to the zero-based iteration value.

备注Remarks

此函数始终用于 copy 对象。This function is always used with a copy object. 如果没有提供任何值作为 偏移量,则返回当前迭代值。If no value is provided for offset, the current iteration value is returned. 迭代值从零开始。The iteration value starts at zero.

loopName 属性可用于指定 copyIndex 是引用资源迭代还是引用属性迭代。The loopName property enables you to specify whether copyIndex is referring to a resource iteration or property iteration. 如果没有为 loopName 提供值,则将使用当前的资源类型迭代。If no value is provided for loopName, the current resource type iteration is used. 在属性上迭代时,请为 loopName 提供值。Provide a value for loopName when iterating on a property.

有关使用 copy 的详细信息,请参阅:For more information about using copy, see:

示例Example

以下示例显示名称中包含 copy 循环和索引值。The following example shows a copy loop and the index value included in the name.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "storageCount": {
            "type": "int",
            "defaultValue": 2
        }
    },
    "resources": [
        {
            "type": "Microsoft.Storage/storageAccounts",
            "apiVersion": "2019-04-01",
            "name": "[concat(copyIndex(),'storage', uniqueString(resourceGroup().id))]",
            "location": "[resourceGroup().location]",
            "sku": {
                "name": "Standard_LRS"
            },
            "kind": "Storage",
            "properties": {},
            "copy": {
                "name": "storagecopy",
                "count": "[parameters('storageCount')]"
            }
        }
    ],
    "outputs": {}
}

返回值Return value

一个表示迭代的当前索引的整数。An integer representing the current index of the iteration.

divdiv

div(operand1, operand2)

返回提供的两个整数在整除后的商。Returns the integer division of the two provided integers.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
operand1operand1 Yes intint 被除数。The number being divided.
operand2operand2 Yes intint 除数。The number that is used to divide. 不能为 0。Can't be 0.

返回值Return value

一个表示商的整数。An integer representing the division.

示例Example

以下示例模板将一个参数除以另一个参数。The following example template divides one parameter by another parameter.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "first": {
            "type": "int",
            "defaultValue": 8,
            "metadata": {
                "description": "Integer being divided"
            }
        },
        "second": {
            "type": "int",
            "defaultValue": 3,
            "metadata": {
                "description": "Integer used to divide"
            }
        }
    },
    "resources": [
    ],
    "outputs": {
        "divResult": {
            "type": "int",
            "value": "[div(parameters('first'), parameters('second'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
divResultdivResult intInt 22

floatfloat

float(arg1)

将值转换为浮点数。Converts the value to a floating point number. 仅当将自定义参数传递给应用程序(例如,逻辑应用)时,才使用此函数。You only use this function when passing custom parameters to an application, such as a Logic App.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
arg1arg1 Yes 字符串或整数string or int 要转换为浮点数的值。The value to convert to a floating point number.

返回值Return value

一个浮点数。A floating point number.

示例Example

以下示例演示如何使用 float 将参数传递给逻辑应用:The following example shows how to use float to pass parameters to a Logic App:

{
    "type": "Microsoft.Logic/workflows",
    "properties": {
        ...
        "parameters": {
            "custom1": {
                "value": "[float('3.0')]"
            },
            "custom2": {
                "value": "[float(3)]"
            },

intint

int(valueToConvert)

将指定的值转换为整数。Converts the specified value to an integer.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
valueToConvertvalueToConvert Yes 字符串或整数string or int 要转换为整数的值。The value to convert to an integer.

返回值Return value

转换后的值的整数。An integer of the converted value.

示例Example

以下示例模板将用户提供的参数值转换为整数。The following example template converts the user-provided parameter value to integer.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "stringToConvert": { 
            "type": "string",
            "defaultValue": "4"
        }
    },
    "resources": [
    ],
    "outputs": {
        "intResult": {
            "type": "int",
            "value": "[int(parameters('stringToConvert'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
intResultintResult intInt 44

maxmax

max (arg1)

返回整数数组或逗号分隔的整数列表中的最大值。Returns the maximum value from an array of integers or a comma-separated list of integers.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
arg1arg1 Yes 整数数组或逗号分隔的整数列表array of integers, or comma-separated list of integers 要获取最大值的集合。The collection to get the maximum value.

返回值Return value

一个整数,表示集合中的最大值。An integer representing the maximum value from the collection.

示例Example

以下示例模板演示如何对整数数组和整数列表使用 max:The following example template shows how to use max with an array and a list of integers:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "arrayToTest": {
            "type": "array",
            "defaultValue": [0,3,2,5,4]
        }
    },
    "resources": [],
    "outputs": {
        "arrayOutput": {
            "type": "int",
            "value": "[max(parameters('arrayToTest'))]"
        },
        "intOutput": {
            "type": "int",
            "value": "[max(0,3,2,5,4)]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
arrayOutputarrayOutput intInt 55
intOutputintOutput intInt 55

minmin

min (arg1)

返回整数数组或逗号分隔的整数列表中的最小值。Returns the minimum value from an array of integers or a comma-separated list of integers.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
arg1arg1 Yes 整数数组或逗号分隔的整数列表array of integers, or comma-separated list of integers 要获取最小值的集合。The collection to get the minimum value.

返回值Return value

一个整数,表示集合中的最小值。An integer representing minimum value from the collection.

示例Example

以下示例模板演示如何对整数数组和整数列表使用 min:The following example template shows how to use min with an array and a list of integers:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "arrayToTest": {
            "type": "array",
            "defaultValue": [0,3,2,5,4]
        }
    },
    "resources": [],
    "outputs": {
        "arrayOutput": {
            "type": "int",
            "value": "[min(parameters('arrayToTest'))]"
        },
        "intOutput": {
            "type": "int",
            "value": "[min(0,3,2,5,4)]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
arrayOutputarrayOutput intInt 00
intOutputintOutput intInt 00

modmod

mod(operand1, operand2)

返回使用提供的两个整数整除后的余数。Returns the remainder of the integer division using the two provided integers.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
operand1operand1 Yes intint 被除数。The number being divided.
operand2operand2 Yes intint 除数,不能为 0。The number that is used to divide, Can't be 0.

返回值Return value

一个表示余数的整数。An integer representing the remainder.

示例Example

以下示例模板将返回一个参数除以另一个参数后所得的余数。The following example template returns the remainder of dividing one parameter by another parameter.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "first": {
            "type": "int",
            "defaultValue": 7,
            "metadata": {
                "description": "Integer being divided"
            }
        },
        "second": {
            "type": "int",
            "defaultValue": 3,
            "metadata": {
                "description": "Integer used to divide"
            }
        }
    },
    "resources": [
    ],
    "outputs": {
        "modResult": {
            "type": "int",
            "value": "[mod(parameters('first'), parameters('second'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
modResultmodResult intInt 11

mulmul

mul(operand1, operand2)

返回提供的两个整数的积。Returns the multiplication of the two provided integers.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
operand1operand1 Yes intint 被乘数。First number to multiply.
operand2operand2 Yes intint 乘数。Second number to multiply.

返回值Return value

一个表示积的整数。An integer representing the multiplication.

示例Example

以下示例模板将一个参数乘以另一个参数。The following example template multiplies one parameter by another parameter.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "first": {
            "type": "int",
            "defaultValue": 5,
            "metadata": {
                "description": "First integer to multiply"
            }
        },
        "second": {
            "type": "int",
            "defaultValue": 3,
            "metadata": {
                "description": "Second integer to multiply"
            }
        }
    },
    "resources": [
    ],
    "outputs": {
        "mulResult": {
            "type": "int",
            "value": "[mul(parameters('first'), parameters('second'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
mulResultmulResult intInt 1515

subsub

sub(operand1, operand2)

返回提供的两个整数在相减后的结果。Returns the subtraction of the two provided integers.

parametersParameters

参数Parameter 必须Required 类型Type 说明Description
operand1operand1 Yes intint 被减数。The number that is subtracted from.
operand2operand2 Yes intint 减数。The number that is subtracted.

返回值Return value

一个表示减后结果的整数。An integer representing the subtraction.

示例Example

以下示例模板将一个参数与另一个参数相减。The following example template subtracts one parameter from another parameter.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "first": {
            "type": "int",
            "defaultValue": 7,
            "metadata": {
                "description": "Integer subtracted from"
            }
        },
        "second": {
            "type": "int",
            "defaultValue": 3,
            "metadata": {
                "description": "Integer to subtract"
            }
        }
    },
    "resources": [
    ],
    "outputs": {
        "subResult": {
            "type": "int",
            "value": "[sub(parameters('first'), parameters('second'))]"
        }
    }
}

上述示例中使用默认值的输出为:The output from the preceding example with the default values is:

名称Name 类型Type ValueValue
subResultsubResult intInt 44

后续步骤Next steps