ARM 範本的數值函式

Resource Manager 提供以下的函式,在 Azure Resource Manager 範本 (ARM 範本) 中可用來處理整數:

提示

我們建議使用 Bicep,因為其提供的功能與 ARM 範本相同,而且語法更易於使用。 若要深入了解如何在 Bicep 中使用 intminmax,請參閱數值函式。 針對其他數值,請參閱數值運算子。

add

add(operand1, operand2)

傳回所提供兩個整數的總和。

Bicep 不支援 add 函式。 請改用+ 運算子

參數

參數 必要 類型​ 描述
operand1 Yes int 要新增的第一個數字。
operand2 Yes 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 中,使用反覆式迴圈

參數

參數 必要 類型​ 描述
loopName No string 用來取得反覆項目的迴圈名稱。
offset No int 要加入到以零為起始之反覆項目值的數字。

備註

這個函式一律搭配 copy 物件使用。 如果未針對 offset 提供任何值,則會傳回目前的反覆項目值。 反覆項目值是從零開始。

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 函式。 請改用/ 運算子

參數

參數 必要 類型​ 描述
operand1 Yes int 被除數。
operand2 Yes 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 函式。

參數

參數 必要 類型​ 描述
arg1 Yes 字串或整數 要轉換為浮點數的值。

傳回值

浮點數。

範例

下列範例顯示如何使用 float 將參數傳遞給邏輯應用程式:

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

int

int(valueToConvert)

將指定的值轉換成整數。

在 Bicep 中,使用 int 函式。

參數

參數 必要 類型​ 描述
valueToConvert Yes 字串或整數 要轉換成整數的值。

傳回值

轉換值的整數。

範例

下列範例範本會將使用者提供的參數值轉換為整數。

{
  "$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

max(arg1)

傳回整數陣列的最大值,或以逗號分隔的整數清單。

在 Bicep 中,使用 max 函式。

參數

參數 必要 類型​ 描述
arg1 Yes 整數的陣列,或以逗號分隔的整數清單 要用來取得最大值的集合。

傳回值

整數,代表集合中的最大值。

範例

下列範例說明在使用 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 函式。

參數

參數 必要 類型​ 描述
arg1 Yes 整數的陣列,或以逗號分隔的整數清單 要用來取得最小值的集合。

傳回值

整數,代表集合中的最小值。

範例

下列範例顯示使用 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 函式。 請改用 % 運算子

參數

參數 必要 類型​ 描述
operand1 Yes int 被除數。
operand2 Yes 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 函式。 請改用 ﹡ 運算子

參數

參數 必要 類型​ 描述
operand1 Yes int 要相乘的第一個數字。
operand2 Yes 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

sub(operand1, operand2)

傳回兩個所提供整數相減後的數字。

Bicep 不支援 sub 函式。 請改用 - 運算子

參數

參數 必要 類型​ 描述
operand1 Yes int 減數。
operand2 Yes 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

下一步