Solved

Powershell System.Object to System.Valuetype

Posted on 2014-12-08
20
360 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
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.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

831 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