Solved

Powershell System.Object to System.Valuetype

Posted on 2014-12-08
20
288 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 28

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 74

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
 

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 28

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 74

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 28

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 68

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 28

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 68

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 28

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 68

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 74

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 28

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 28

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now