如何将货币字符串转换为数值?

6

有没有一种方法将货币字符串转换为浮点值,例如:

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

但这些只是将成本提取为字符串值。
3个回答

9
$Test = '$1,138.15','$ 29.10','$2,195.34'

$Test |
 foreach { $_ -replace '[^0-9.]'}


1138.15
29.10
2195.34

如果你想保留尾部的0,则需要将其保留为[string],直到进行任何所需计算。

1
更简洁地说,$Test -replace '[^\d.]' - Adi Inbar

5

您可以使用 Replace 方法从字符串中删除任何要去除的额外内容,然后将其转换为 [decimal] 类型。

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

编辑:当然,mjolinor已经击败了我,并且做得更好。他真的很棒!


0

尽管这篇帖子已经有了答案,但是这个答案没有处理由()-指示的负值。由于这是在谷歌搜索如何将货币字符串解析为PowerShell中的十进制值时弹出的问题,我想我会留下一份更新。

为了简洁起见,在代码示例中,我直接使用枚举int值而不是枚举名称。

[System.Globalization.NumberStyles]

不应该使用 383,而是应该使用:
[System.Globalization.NumberStyles]::Currency

使用[decimal]::TryParse()方法 - 该方法返回一个布尔值,指示解析尝试的状态。如果成功,方法返回$True并将引用变量填充为解析后的值;否则,方法返回$False并将引用变量设置为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

使用[decimal]::Parse() - 要么成功,要么抛出异常。
[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'

这个答案是从一个类似的C#答案改编而来的: 将货币字符串转换为十进制数? 给未来的读者一个小提示,如果你在PowerShell标签下找不到你需要的答案,试着在C# / .NET下搜索。你很可能能在那里找到答案,然后你可以将答案改编成适用于PowerShell的形式,这就是我得出这个答案的方法。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接