Question:
Is there a way to convert currency strings to floating values, for example:
1 2 3 4 |
$1,138.15 $ 29.10 $2,195.34 |
Should be converted to:
1 2 3 4 |
1138.15 29.10 2195.34 |
Some currency strings have space between the dollar sign and the dollar value.
I am doing this because I am extracting the cost values from PDFs (which are converted to txt files) and do arithmetic on them. For example, a portion of text file looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 |
Fixed Power $1,138.15 General Admin Surcharge $ 29.10 Customer Charge $2,195.34 |
And my code looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
$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] |
But those only extract the costs into string values.
Answer:
1 2 3 4 5 6 7 8 9 10 |
$Test = '$1,138.15','$ 29.10','$2,195.34' $Test | foreach { $_ -replace '[^0-9.]'} 1138.15 29.10 2195.34 |
If you want the trailing 0 you’ll have to leave it as [string] until you do whaterver calculations you want with it.