我试图使用 .ToString()
格式化数字:如果我有4.44在字符串之后,数字必须为004.4400。当我单独使用 .ToString()
时,一切正常工作,例如:
$B = 4.444
$C = $B.ToString("000.0000") -replace "," , "."
但是,当我尝试在周期中使用.ToString()
时,一切都出错了,我不知道为什么。
这是我的脚本:
$path = "C:UsersgeorgiDesktop5"
Get-ChildItem -Path "C:UsersgeorgiDesktop5" -PipelineVariable 'f' |
ForEach-Object{
$Program_Name = "LH2113814_11111-300"
$PrepareDate = (Get-Content -Path $f.FullName) | Select -Index 9
$Date = $PrepareDate.Substring(31,4) + $PrepareDate.Substring(28,2) +
$PrepareDate.Substring(25,2) + $PrepareDate.Substring(36,8)
$A = (Get-Content -Path $f.FullName) | Select -Index 14
$B = $A.Substring(43,5)
$C = $B.ToString("000.0000") -replace ",","."
$D = (Get-Content -Path $f.FullName) | Select -Index 20
$E = $D.Substring(37,5)
$F = "00" + $E + "0"
$EXPORT = $Program_Name + $Date + $C + $F | Add-Content
('C:UsersgeorgiDesktop5test.dat')
}
错误:
Cannot find an overload for "ToString" and the argument count: "1".
At line:8 char:12
+ $B.ToString("000.0000") -replace ".",","
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
在这些行中:
$B = $A.Substring(43,5)
$C = $B.ToString("000.0000") -replace ",","."
$B
是String
类型。该类型的ToString()
成员函数不接受任何参数,这就是为什么它抱怨的原因。
我无法根据您发布的内容来分辨,但是也许您需要先将该字符串施放到float
:
$B = [float]$A.Substring(43,5)
$C = $B.ToString("000.0000") -replace ",","."