6

Is there a way to convert currency strings to floating values, for example:

$1,138.15
$ 29.10
$2,195.34

Should be converted to:

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:

Fixed Power

$1,138.15

General Admin Surcharge

$ 29.10

Customer Charge

$2,195.34

And my code looks like this:

$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.

3 Answers 3

9
$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.

1
  • 1
    ...or more succinctly, $Test -replace '[^\d.]'.
    – Adi Inbar
    Commented Jul 18, 2014 at 22:08
5

You could do a Replace to remove whatever extra things you want gone from the string, and then cast it as a [decimal]

[decimal]$customerCharge = $customerCharge -replace "(\$| |,)"

Edit: Of coarse mjolinor beat me to it, and did it better. Man he's good!

0
0

Even though this post has an answer already, the answer does not handle negative values indicated by either () or -. Since this is the question that pops up when googling how to parse a currency string into a decimal value in PowerShell, I figured I would leave an update.

For brevity in the code examples, I am using the enum int value directly rather than the enum name.

[System.Globalization.NumberStyles]

Instead of 383, you should really be using:

[System.Globalization.NumberStyles]::Currency

Using [decimal]::TryParse() - method returns bool indicating status of parsing attempt, if successful, method returns $True and the reference variable is populated with the parsed value, otherwise the method returns $False and the reference variable is set to 0.

New-Variable -Name test -Force
$null = [decimal]::TryParse('-$ 1,200.20' , 383, (Get-Culture).NumberFormat, [ref]$test); $test
$null = [decimal]::TryParse('($ 1,200.20)', 383, (Get-Culture).NumberFormat, [ref]$test); $test
$null = [decimal]::TryParse('$1,200.20'   , 383, (Get-Culture).NumberFormat, [ref]$test); $test
$null = [decimal]::TryParse('$1200.20'    , 383, (Get-Culture).NumberFormat, [ref]$test); $test
$null = [decimal]::TryParse('$ 1200.2'    , 383, (Get-Culture).NumberFormat, [ref]$test); $test
$null = [decimal]::TryParse('$1200'       , 383, (Get-Culture).NumberFormat, [ref]$test); $test

Using [decimal]::Parse() - Either is successful, or throws an exception.

[decimal]::Parse('-$ 1,200.20' , 383, (Get-Culture).NumberFormat) # Returns '-1200.20'
[decimal]::Parse('($ 1,200.20)', 383, (Get-Culture).NumberFormat) # Returns '-1200.20'
[decimal]::Parse('$(1,200.20)' , 383, (Get-Culture).NumberFormat) # Returns '-1200.20'
[decimal]::Parse('$1,200.20'   , 383, (Get-Culture).NumberFormat) # Returns '1200.20'
[decimal]::Parse('$1200.20'    , 383, (Get-Culture).NumberFormat) # Returns '1200.20'
[decimal]::Parse('$ 1200.2'    , 383, (Get-Culture).NumberFormat) # Returns '1200.2'
[decimal]::Parse('$1200'       , 383, (Get-Culture).NumberFormat) # Returns '1200'

This answer was adapated from a similar answer for C#: Convert currency string to decimal?

Just a tip to future readers, if you can't find the answer to what you need under the PowerShell tag, try searching under C# / .NET. You'll likely be able to find the answer there, and then you can adapt the answer to use in PowerShell, which is how I came up with this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.