Solved

Powershell System.Object to System.Valuetype

Posted on 2014-12-08
20
396 Views
Last Modified: 2014-12-08
Hello,

Im trying to import a number from text file.
So I have created this:

$Subtotal = $text | Select-String -Pattern Subtotal | Select-Object  -First 1
$Subtotal = $Subtotal.line
$Subtotal = $Subtotal.trim("Subtotal ")
$Transportcost = $text | Select-String -Pattern Transportcost | Select-Object  -First 1
$Transportcost = $Transportcost.line
$Transportcost = $Transportcost.trim("Transportcost ")

now I need the sum of $subtotal + $transport but that doesn't work as $subtotal and $transportcost are system.objects

How do I convert?

Regards

Hans
0
Comment
Question by:Hans de Jongh
  • 7
  • 7
  • 3
  • +1
20 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 40486668
Simply cast as the object type you want e.g.

$sum = [int]$subtotal + [int]$transportcost


I might be a bit off but this is the path
0
 

Author Comment

by:Hans de Jongh
ID: 40486681
yeah tried that already

PS C:\Users\user>
PS C:\Users\user>
PS C:\Users\user> $Subtotal = $text | Select-String -Pattern Subtotal | Select-Object  -First 1
PS C:\Users\user> $Subtotal = $Subtotal.line
PS C:\Users\user> $Subtotal = $Subtotal.trim("Subtotal ")
PS C:\Users\user> $Transportcost = $text | Select-String -Pattern Transportcost | Select-Object  -First 1
PS C:\Users\user> $Transportcost = $Transportcost.line
PS C:\Users\user> $Transportcost = $Transportcost.trim("Transportcost ")
PS C:\Users\user> $Transportcost
0,00
PS C:\Users\user> $Subtotal
1.738,59
PS C:\Users\user> $sum = [int]$Subtotal + [int]$Transportcost
Cannot convert value "1.738,59" to type "System.Int32". Error: "Input string was not in a correct format."
At line:1 char:1
+ $sum = [int]$Subtotal + [int]$Transportcost
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastFromStringToInteger

PS C:\Users\user>
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40486691
You need to use Convert instead. Casting is not appropriate here.

$sum = [System.Convert]::ToInt32($Subtotal) + [System.Convert]::ToInt32($Transportcost)

Open in new window


You may desire ToDouble/ToDecimal over ToInt32.
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:Hans de Jongh
ID: 40486707
tried that too:0 (well I tried int64)

Exception calling "ToInt32" with "1" argument(s): "Input string was not in a correct format."
At line:1 char:1
+ $sum = [System.Convert]::ToInt32($Subtotal) + [System.Convert]::ToInt32($Transp ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FormatException
0
 
LVL 29

Expert Comment

by:becraig
ID: 40486710
So I am guessing seeing the values you are working with would help.

Here is a working example of what I suggested:

PS C:\Users\Administrator\Desktop> $transportcost = "Transportcost 100.67"
PS C:\Users\Administrator\Desktop> $Transportcost = $Transportcost -replace "Transportcost ",""
PS C:\Users\Administrator\Desktop> $subtotal = "Subtotal 200.89"
PS C:\Users\Administrator\Desktop> $subtotal = $subtotal -replace "subtotal",""
PS C:\Users\Administrator\Desktop> $sum = ([int]$subtotal) + ([int]$transportcost)
PS C:\Users\Administrator\Desktop> $sum
302
PS C:\Users\Administrator\Desktop>

Open in new window



I am guessing we need to see the actual string input that would help to determine what manipulation needs to be made.

If these are monetary values there should be no issue casting as int
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40486719
Are you certain the values in the two variables are numeric? "Input string was not in a correct format" implies that they are not. Keep in mind that empty string counts as not numeric also.
0
 

Author Comment

by:Hans de Jongh
ID: 40486726
no they are not that's why im trimming them

PS C:\Users\user> $Subtotaal = $text | Select-String -Pattern Subtotaal | Select-Object  -First 1
PS C:\Users\user> $subtotaal

Subtotaal 1.738,59


PS C:\Users\user>  $Transportkosten = $text | Select-String -Pattern Transportkosten | Select-Object  -First 1
PS C:\Users\user> $Transportkosten

Transportkosten 0,00


PS C:\Users\user>
0
 
LVL 29

Expert Comment

by:becraig
ID: 40486727
Kaufmed, that is why I am asking for a sample of the input as well.

As indicated in my post, there is no reason for this to work other than the inputs not being in the expected string format.

So instead of fixing this using one method having a clearer picture can resolve any other issues once we know the varied formats he will be importing.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40486732
Both should have worked for a properly formatted string. No other characters than digits,  minus, plus, dot and spaces are allowed. Not sure about thousands separator, though.
0
 
LVL 29

Expert Comment

by:becraig
ID: 40486734
Ok so the real question here (thinking in terms of currency) is what will 1.738,59 express.

Will it be $1,738.59 ?



The modification can be made but the final format should express the value clearly.
The input we have issue with is the "comma" however we can resolve that but it need to be clear how the final number will be read.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40486737
Obviously you need to replace decimal comma by dot. Is that Afrkaans or Dutch?
And prior to that remove the thousands dot.
0
 

Author Comment

by:Hans de Jongh
ID: 40486741
im going to fill a sharepoint item with that number.
I prefer to keep it as clean as possible
So just a number with 2 digits after the ","  is fine.
I don't need the thousands separator
0
 

Author Comment

by:Hans de Jongh
ID: 40486744
im dutch:)
0
 
LVL 29

Expert Comment

by:becraig
ID: 40486748
So here is what you might need to do (replace the dot with nothing, replace the comma with a dot)
PS C:\Users\Administrator\Desktop> $transportcost = "Transportcost 1867.48"
PS C:\Users\Administrator\Desktop> $Transportcost = $Transportcost -replace "Transportcost ",""
PS C:\Users\Administrator\Desktop> $subtotal = "Subtotal 2879.74"
PS C:\Users\Administrator\Desktop> $subtotal = $subtotal -replace "subtotal",""
PS C:\Users\Administrator\Desktop> $sum = ([int]$subtotal) + ([int]$transportcost)
PS C:\Users\Administrator\Desktop> $sum
4747

Open in new window

So 1.738,59  becomes 1738.59

You can of course cast the string as needed to get all the decimal places you need.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40486751
Btw, System only contains types, it isn't one itself. System.String and System.Int32 are types.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40486757
You may need to introduce some of the culture classes here, though I should have expected .NET to pick up your system's settings when doing the convert. You can pass a IFormatProvider as the second argument to ToDecimal (et al.):

$culture = [System.Globalization.CultureInfo]::CreateSpecificCulture("nl-NL")
$sum = [System.Convert]::ToDecimal($Subtotal, $culture.NumberFormat) + [System.Convert]::ToDecimal($Transportcost, $culture.NumberFormat)

Open in new window

0
 
LVL 29

Expert Comment

by:becraig
ID: 40486758
You can also just cast as decimal to keep you value in tact:
$sum = ([decimal]$subtotal) + ([decimal]$transportcost)

Open in new window

0
 

Author Comment

by:Hans de Jongh
ID: 40486759
the current string is

transportcost 1.234,56

so how would this then work

$Transportcost = $Transportcost -replace "Transportcost ",""

that would make it
1.23456
or 123456

I think?
0
 
LVL 29

Expert Comment

by:becraig
ID: 40486770
Good catch Kaufmed I was just checking the culture format for Dutch but you beat me to it.

What you have above should work perfectly, (my limited number formats here in the US sometimes clouds my thinking)

Kaufmed's code should work perfectly, once you specify the culture in your shell, PS should be able to work with the numeric values you input.


PS C:\Users\Administrator\Desktop> $culture = [System.Globalization.CultureInfo]::CreateSpecificCulture("nl-NL")
PS C:\Users\Administrator\Desktop> $subtotal = "Subtotal 2.879,74"
PS C:\Users\Administrator\Desktop> $transportcost = "Transportcost 1.867,48"
PS C:\Users\Administrator\Desktop> $subtotal = $subtotal -replace "subtotal",""
PS C:\Users\Administrator\Desktop> $Transportcost = $Transportcost -replace "Transportcost ",""
PS C:\Users\Administrator\Desktop> $sum = [System.Convert]::ToDecimal($Subtotal, $culture.NumberFormat) + [System.Convert]::ToDecimal($Transportcost, $culture.NumberFormat)
PS C:\Users\Administrator\Desktop> $sum
4747.22

Open in new window

0
 

Author Closing Comment

by:Hans de Jongh
ID: 40486779
thanks!
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
A procedure for exporting installed hotfix details of remote computers using powershell
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question