[英]Convert string to int for subtraction
這是我的代碼,
$fourCount = 0
$rowCount = 0
foreach($row in $ResultsTable){
if ($row.'RowType' -eq 6) {
if ($fourCount -gt 0) {
#Sort-Object {$row.'CheckDate'} - descending
$remainAmount = $currentAmount - $checkAmount
$ResultsTable.Rows[$rowCount - 1].'Final' = $remainAmount
$currentAmount = @()
$fourCount = 0
$remainAmount = 0
}
$checkAmount = [int]$row.'amount'
$rowCount = $rowCount + 1
} elseif ($row.'RowType' -eq 4) {
if($row.'current'.Length -eq $NULL) {
Continue
} else {
$currentAmount += [int]$row.'current'
$rowCount = $rowCount + 1
$fourCount++
}
} else {
Continue
}
}
我運行代碼時遇到的問題是
方法調用失敗,因為[System.Object []]不包含名為“ op_Subtraction”的方法。
$ remainAmount = $ currentAmount-<<<< $ checkAmount CategoryInfo:InvalidOperation:(op_Subtraction:String)[],RuntimeException
FullyQualifiedErrorId:MethodNotFound
從我讀到的內容來看,我正在將所有內容都轉換為一個int,但仍然無法運行。 誰能告訴我為什么?
在第9行,您將$ currentAmount初始化為一個空數組:
$currentAmount = @()
然后,您將[int]個項目累積到該數組中:
$currentAmount += [int]$row.'current'
因此,您所擁有的是[int]數組。
最終,您嘗試對該數組進行減法運算:
$remainAmount = $currentAmount - $checkAmount
它失敗了。
錯誤消息中的[Type []]語法表明它是一個數組,您不能對此進行減法運算。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.