Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 674
  • Last Modified:

Powershell System.Object to System.Valuetype

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
Hans de Jongh
Asked:
Hans de Jongh
  • 7
  • 7
  • 3
  • +1
1 Solution
 
becraigCommented:
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
 
Hans de JonghAuthor Commented:
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
 
käµfm³d 👽Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Hans de JonghAuthor Commented:
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
 
becraigCommented:
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
 
käµfm³d 👽Commented:
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
 
Hans de JonghAuthor Commented:
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
 
becraigCommented:
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
 
QlemoC++ DeveloperCommented:
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
 
becraigCommented:
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
 
QlemoC++ DeveloperCommented:
Obviously you need to replace decimal comma by dot. Is that Afrkaans or Dutch?
And prior to that remove the thousands dot.
0
 
Hans de JonghAuthor Commented:
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
 
Hans de JonghAuthor Commented:
im dutch:)
0
 
becraigCommented:
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
 
QlemoC++ DeveloperCommented:
Btw, System only contains types, it isn't one itself. System.String and System.Int32 are types.
0
 
käµfm³d 👽Commented:
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
 
becraigCommented:
You can also just cast as decimal to keep you value in tact:
$sum = ([decimal]$subtotal) + ([decimal]$transportcost)

Open in new window

0
 
Hans de JonghAuthor Commented:
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
 
becraigCommented:
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
 
Hans de JonghAuthor Commented:
thanks!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 7
  • 7
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now