int

概要

从输入字符串或整数返回整数。

语法

int(<inputValue>)

描述

int() 函数返回整数,将输入字符串转换为整数。 如果传递整数,则返回整数。 如果传递任何其他值(包括非整数数),该函数将引发无效的输入错误。

例子

示例 1 - 从字符串创建整数

此配置返回一个整数,将字符串值 '4.7' 转换为 4

# int.example.1.dsc.config.yaml
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
resources:
- name: Echo integer value
  type: Test/Echo
  properties:
    output: "[int('4.7')]"
dsc config get --document int.example.1.dsc.config.yaml config get
results:
- name: Echo integer value of '4.7'
  type: Test/Echo
  result:
    actualState:
      output: 4
messages: []
hadErrors: false

参数

inputValue

int() 函数需要输入作为字符串或整数。 如果该值是无法分析为数字 DSC 的字符串,则返回函数的错误。 如果值不是字符串或整数,DSC 将返回函数的输入错误无效。

Type:         [String, Integer]
Required:     true
MinimumCount: 1
MaximumCount: 1

输出

int() 函数返回输入的整数表示形式。 如果输入值为带小数部分的字符串或数字,则函数返回没有小数部分的整数。 它不会四舍五入,因此对于 4.999 的输入值,函数返回 4

Type: integer