将数字格式化为4位数在Azure管道中

huangapple go评论130阅读模式
英文:

format numbers to 4 digits in Azure Pipline

问题

我尝试在Azure管道(YAML)中格式化4位数字,但不起作用,有人可以帮忙吗?

我有这个变量 >> serialNumber: 2

我使用了以下yaml语法:

formatnumber: $[format('{0:D4}', variables.serialNumber)]

formatnumber: $[format('{0:0000}', variables.serialNumber)]

formatnumber: $[format('{0:####}', variables.serialNumber)]

我想将数字2转换为0002并进行格式化,但我收到了以下错误:

加载YAML构建管道时发生错误。格式说明符'D4'对于类型'String'的对象无效。

英文:

I try to format 4 digits in Azure pipeline (YAML) but its not working, someone help?

i have this variable >> serialNumber: 2

I used the following yaml syntax:

formatnumber: $[format('{0:D4}', variables.serialNumber)]

formatnumber: $[format('{0:0000}', variables.serialNumber)]

formatnumber: $[format('{0:####}', variables.serialNumber)]

i want to convert the number 2 to 0002 with format but i get the following error:

>An error occurred while loading the YAML build pipeline. The format specifiers 'D4' are not valid for objects of type 'String'

答案1

得分: 2

以上格式化器不起作用是因为variables.serialNumber返回的值的类型是字符串而不是整数类型。

有一种解决方法可以实现这个目标。您可以添加一个PowerShell任务来格式化数字。您可以查看下面的示例。

trigger:
- master
variables:
  number: 2
pool:
  vmImage: 'ubuntu-latest'
steps:
- powershell: |
    $number = $(number)
    $format = "{0:0000}" -f $number
    echo "##vso[task.setvariable variable=formatnumber]$format"
        
- powershell: echo "$(formatnumber)"

上面的PowerShell任务在格式化变量number后,将格式化后的数字设置为变量'formatnumber'。然后,下面的任务可以通过$(formatnumber)引用格式化后的数字。

英文:

The above formater doesnot work is because the type of the value returned by variables.serialNumber is string not int type.

There is a workaround to achieve this. You can add a powershell task to format the number. You can check below example.

trigger:
- master
variables:
  number: 2
pool:
  vmImage: 'ubuntu-latest'
steps:
- powershell: |
    $number = $(number)
    $format = "{0:0000}" -f $number
    echo "##vso[task.setvariable variable=formatnumber]$format"

- powershell: echo "$(formatnumber)"

Above script in powershell task formats the variable number, and set the formatted number to variable 'formatnumber'. Then the following task can refer to the formatted number by $(formatnumber)

huangapple
  • 本文由 发表于 2020年1月6日 18:13:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610210.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定