# how to convert String to int or another datatype

**URL:** <https://forums.powershell.org/t/how-to-convert-string-to-int-or-another-datatype/2011>\
**Category:** PowerShell Help\
**Created:** [October 28, 2013, 8:39pm UTC](https://forums.powershell.org/t/how-to-convert-string-to-int-or-another-datatype/2011 "2013-10-28T20:39:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dominique-hofmann](https://avatars.discourse-cdn.com/v4/letter/d/ecccb3/32.png) [@dominique-hofmann](https://forums.powershell.org/u/dominique-hofmann)\
**Post date:** [October 28, 2013, 8:39pm UTC](https://forums.powershell.org/t/how-to-convert-string-to-int-or-another-datatype/2011/1 "2013-10-28T20:39:43Z")

</div>

Hello together  
my question is how can i convert an variable $Size (content in attachment) from String to another datatype like integer or string

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [October 28, 2013, 11:52pm UTC](https://forums.powershell.org/t/how-to-convert-string-to-int-or-another-datatype/2011/2 "2013-10-28T23:52:19Z")

</div>

We can’t tell from that screenshot exactly what type $Size was; it could have been either String, Double, or Decimal. Based on your other thread, I would guess it was Double.

There are different ways of converting variables to other types. Which one you choose depends mainly on how you want to handle errors, and if you’re willing to accept an automatic loss of precision. For example, here are some different ways you might try to attempt to convert the value 2.9055827878416 to an Integer (which cannot contain a decimal portion):

```
$double = [double]2.9055827878416

[int]$double # Results in integer 2. The decimal portion of the double is simply discarded (not rounded)
[int][math]::Floor($double) # Same as above.
[int]::Parse($double) # This produces an error, because the double value (which is implicitly casted to a string first) contains a decimal portion.
[int][math]::Round($double) # Results in integer 3, since we rounded the value up to 3.0 as a Double first.
```

---

<div class="post-metadata">

**Author:** ![dominique-hofmann](https://avatars.discourse-cdn.com/v4/letter/d/ecccb3/32.png) [@dominique-hofmann](https://forums.powershell.org/u/dominique-hofmann)\
**Post date:** [October 29, 2013, 12:19am UTC](https://forums.powershell.org/t/how-to-convert-string-to-int-or-another-datatype/2011/3 "2013-10-29T00:19:43Z")

</div>

thank you again again

---

<div class="post-metadata">

**Author:** ![richard-siddaway](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@richard-siddaway](https://forums.powershell.org/u/richard-siddaway)\
**Post date:** [October 29, 2013, 12:53am UTC](https://forums.powershell.org/t/how-to-convert-string-to-int-or-another-datatype/2011/4 "2013-10-29T00:53:05Z")

</div>

You can also use the -as operator

$a = “122”  
$b = $a -as [int]

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:56pm UTC](https://forums.powershell.org/t/how-to-convert-string-to-int-or-another-datatype/2011/5 "2024-05-16T20:56:58Z")

</div>

Closed on May 16, 2024, 8:56pm UTC


