有没有一种方法将货币字符串转换为浮点值,例如:
$1,138.15
$ 29.10
$2,195.34
Should be converted to:
1138.15
29.10
2195.34
一些货币字符串在美元符号和美元金额之间有空格。
我这样做是因为我要从PDF中提取成本值(它们被转换为txt文件)并对其进行算术运算。例如,文本文件的一部分如下:
Fixed Power
$1,138.15
General Admin Surcharge
$ 29.10
Customer Charge
$2,195.34
我的代码长这样:
$sourceFile = Get-Content $sourcePath\$($filenames[0]).txt
$fixedPower = $sourceFile[(
Select-String `
-Path $sourcePath\$($filenames[0]).txt `
-Pattern "Fixed Power" `
-List
).LineNumber + 1]
$generalAdminSurcharge = $sourceFile[(
Select-String `
-Path $sourcePath\$($filenames[0]).txt `
-Pattern "General Admin Surcharge" `
-List
).LineNumber + 1]
$customerCharge = $sourceFile[(
Select-String `
-Path $sourcePath\$($filenames[0]).txt `
-Pattern "Customer Charge" `
-List
).LineNumber + 1]
但这些只是将成本提取为字符串值。
$Test -replace '[^\d.]'
。 - Adi Inbar