Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert currency strings to numerical values?

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.

like image 522
vxs8122 Avatar asked Jul 18 '14 16:07

vxs8122


People also ask

How do I convert a string to a number?

You convert a string to a number by calling the Parse or TryParse method found on numeric types ( int , long , double , and so on), or by using methods in the System.Convert class.

Which function converts a string value into numeric value?

Another way to convert a string into a number is to use the parseInt() function. This function takes in a string and an optional radix. A radix is a number between 2 and 36 which represents the base in a numeral system.

How do I convert a string to numeric data in Python?

We will be using . LabelEncoder() from sklearn library to convert categorical data to numerical data. We will use function fit_transform() in the process.

How do you convert currency in Python?

Let's see a Python program to convert the currency of one country to that of another country. To use this service, one must need the API key, which can be get from here. We will use fixer API to get the live conversion rates and convert the corresponding amount.


2 Answers

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

like image 104
mjolinor Avatar answered Sep 24 '22 21:09

mjolinor


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!

like image 27
TheMadTechnician Avatar answered Sep 21 '22 21:09

TheMadTechnician