你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
资源管理器提供以下函数,用于在 Azure 资源管理器模板(ARM 模板)中使用整数:
小窍门
建议使用 Bicep,因为它提供与 ARM 模板相同的功能,并且语法更易于使用。 若要详细了解如何使用 intBicep min和 max Bicep,请参阅 numeric 函数。 有关其他数值,请参阅 数值 运算符。
添加
add(operand1, operand2)
返回两个提供的整数的总和。
Bicep 不支持 add 函数。 请改用 + 运算符 。
参数
| 参数 | 必选 | 类型 | DESCRIPTION | 
|---|---|---|---|
| operand1 | 是的 | 整数 (int) | 要添加的第一个数字。 | 
| operand2 | 是的 | 整数 (int) | 要添加的第二个数字。 | 
返回值
一个包含参数总和的整数。
示例:
以下示例添加两个参数:
{
  "$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'))]"
    }
  }
}
上述示例中的默认值输出为:
| 名称 | 类型 | 价值 | 
|---|---|---|
| addResult | int (整数) | 8 | 
copyIndex
copyIndex(loopName, offset)
返回迭代循环的索引。
在 Bicep 中,使用 迭代循环。
参数
| 参数 | 必选 | 类型 | DESCRIPTION | 
|---|---|---|---|
| loopName | 否 | 字符串 | 用于获取迭代的循环的名称。 | 
| 偏移 | 否 | 整数 (int) | 要添加到从零开始的迭代值的数字。 | 
注解
此函数始终与 复制 对象一起使用。 如果未为 偏移量提供任何值,则返回当前迭代值。 迭代值从零开始。
              通过 loopName 属性,可以指定引用copyIndex资源迭代还是属性迭代。 如果未为 loopName 提供任何值,则使用当前资源类型迭代。 在对属性进行循环访问时为 loopName 提供值。
有关使用复制的详细信息,请参阅:
示例:
以下示例演示名称中包含的复制循环和索引值:
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageCount": {
      "type": "int",
      "defaultValue": 2
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]"
    }
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2022-09-01",
      "name": "[format('{0}storage{1}', range(0, parameters('storageCount'))[copyIndex()], uniqueString(resourceGroup().id))]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Standard_LRS"
      },
      "kind": "Storage",
      "properties": {},
      "copy": {
        "name": "storagecopy",
        "count": "[parameters('storageCount')]"
      }
    }
  ]
}
返回值
一个整数,表示迭代的当前索引。
div
div(operand1, operand2)
返回两个提供的整数的整数除法。
Bicep 不支持 div 函数。 请改用 / 运算符 。
参数
| 参数 | 必选 | 类型 | DESCRIPTION | 
|---|---|---|---|
| operand1 | 是的 | 整数 (int) | 要除数的数字。 | 
| operand2 | 是的 | 整数 (int) | 用于除数的数字。 不能为 0。 | 
返回值
一个表示除法的整数。
示例:
以下示例将一个参数除以另一个参数:
{
  "$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'))]"
    }
  }
}
上述示例中的默认值输出为:
| 名称 | 类型 | 价值 | 
|---|---|---|
| divResult | int (整数) | 2 | 
float
float(arg1)
将值转换为浮点数。 仅当将自定义参数传递给应用程序(例如逻辑应用)时,才使用此函数。
Bicep 不支持 float 函数。
参数
| 参数 | 必选 | 类型 | DESCRIPTION | 
|---|---|---|---|
| arg1 | 是的 | 字符串或整数 | 要转换为浮点数的值。 | 
返回值
浮点数。
示例:
以下示例演示如何使用 float 将参数传递给逻辑应用:
{
  "type": "Microsoft.Logic/workflows",
  "properties": {
    ...
    "parameters": {
      "custom1": {
        "value": "[float('3.0')]"
      },
      "custom2": {
        "value": "[float(3)]"
      },
整数 (int)
int(valueToConvert)
将指定的值转换为整数。
在 Bicep 中,使用函数 int 。
参数
| 参数 | 必选 | 类型 | DESCRIPTION | 
|---|---|---|---|
| valueToConvert | 是的 | 字符串或整数 | 要转换为整数的值。 | 
返回值
已转换值的整数。
示例:
以下示例模板将用户提供的参数值转换为整数:
{
  "$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'))]"
    }
  }
}
上述示例中的默认值输出为:
| 名称 | 类型 | 价值 | 
|---|---|---|
| intResult | int (整数) | 4 | 
最大值
max(arg1)
返回整数数组或逗号分隔的整数列表中的最大值。
在 Bicep 中,使用函数 max 。
参数
| 参数 | 必选 | 类型 | DESCRIPTION | 
|---|---|---|---|
| arg1 | 是的 | 整数数组或逗号分隔的整数列表 | 要获取最大值的集合。 | 
返回值
一个整数,表示集合中的最大值。
示例:
以下示例演示如何与数组和整数列表一 max 起使用:
{
  "$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)]"
    }
  }
}
上述示例中的默认值输出为:
| 名称 | 类型 | 价值 | 
|---|---|---|
| arrayOutput | int (整数) | 5 | 
| intOutput | int (整数) | 5 | 
分钟
min(arg1)
返回整数数组或逗号分隔的整数列表中的最小值。
在 Bicep 中,使用函数 min 。
参数
| 参数 | 必选 | 类型 | DESCRIPTION | 
|---|---|---|---|
| arg1 | 是的 | 整数数组或逗号分隔的整数列表 | 要获取最小值的集合。 | 
返回值
一个整数,表示集合中的最小值。
示例:
以下示例演示如何与数组和整数列表一 min 起使用:
{
  "$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)]"
    }
  }
}
上述示例中的默认值输出为:
| 名称 | 类型 | 价值 | 
|---|---|---|
| arrayOutput | int (整数) | 0 | 
| intOutput | int (整数) | 0 | 
mod
mod(operand1, operand2)
使用提供的两个整数返回整数除法的商。
Bicep 不支持 mod 函数。 请改用 % 运算符 。
参数
| 参数 | 必选 | 类型 | DESCRIPTION | 
|---|---|---|---|
| operand1 | 是的 | 整数 (int) | 要除数的数字。 | 
| operand2 | 是的 | 整数 (int) | 用于除法的数字不能为 0。 | 
返回值
一个表示余数的整数。
示例:
以下示例返回将一个参数除以另一个参数的商:
{
  "$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'))]"
    }
  }
}
上述示例中的默认值输出为:
| 名称 | 类型 | 价值 | 
|---|---|---|
| modResult | int (整数) | 1 | 
mul
mul(operand1, operand2)
返回两个提供的整数的乘法。
Bicep 不支持 mul 函数。 请改用 * 运算符 。
参数
| 参数 | 必选 | 类型 | DESCRIPTION | 
|---|---|---|---|
| operand1 | 是的 | 整数 (int) | 要相乘的第一个数字。 | 
| operand2 | 是的 | 整数 (int) | 要相乘的第二个数字。 | 
返回值
一个表示乘法的整数。
示例:
以下示例将一个参数乘以另一个参数:
{
  "$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(mul(parameters('first'), parameters('second')), 3)]"
    }
  }
}
上述示例中的默认值输出为:
| 名称 | 类型 | 价值 | 
|---|---|---|
| mulResult | int (整数) | 45 | 
子项
sub(operand1, operand2)
返回两个提供的整数的减法。
Bicep 不支持 sub 函数。 请改用 - 运算符 。
参数
| 参数 | 必选 | 类型 | DESCRIPTION | 
|---|---|---|---|
| operand1 | 是的 | 整数 (int) | 从中减去的数字。 | 
| operand2 | 是的 | 整数 (int) | 减去的数字。 | 
返回值
一个表示减法的整数。
示例:
以下示例从另一个参数中减去一个参数:
{
  "$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'))]"
    }
  }
}
上述示例中的默认值输出为:
| 名称 | 类型 | 价值 | 
|---|---|---|
| subResult | int (整数) | 4 | 
后续步骤
- 有关 ARM 模板中各部分的说明,请参阅了解 ARM 模板的结构和语法。
- 若要在创建资源类型时迭代指定的次数,请参阅 ARM 模板中的资源迭代。