Solved

Powershell System.Object to System.Valuetype

Posted on 2014-12-08
20
517 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

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 70

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 70

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 70

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

632 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