VMware Cloud Community
Bigi201110141
Enthusiast
Enthusiast
Jump to solution

Decimal round of size

Here is a portion of my script. Can someone please assist with the rounding of the sizemb on the last line

When I run this I am getting the following

34.457454758739874837483

I would like it to round of to the nearest MB or 1 decimal place (either 34MB or 34.5MB)

Big Thanks.

$VMsWithSnaps = @(Get-VM | Get-Snapshot | Select vm,name,sizemb)

if($VMsWithSnaps -ne $null){

  $body = @("

  <center><table border=1 width=60% cellspacing=0 cellpadding=8 bgcolor=Black cols=3>

  <tr bgcolor=White><td>Virtual Machine</td><td>Snapshot</td><td>Size in MB</td></tr>")

  $i = 0

  do {

  if($i % 2){$body += "<tr bgcolor=#D2CFCF><td>$($VMsWithSnaps[$i].VM)</td><td>$($VMsWithSnaps[$i].Name)</td><td>$($VMsWithSnaps[$i].Sizemb)</td></tr>";$i++}

1 Solution

Accepted Solutions
mattandes
Enthusiast
Enthusiast
Jump to solution

Try replacing this line:


$VMsWithSnaps = @(Get-VM | Get-Snapshot | Select vm,name,sizemb)


With this:


$VMsWithSnaps = @(Get-VM | Get-Snapshot | Select vm,name,@{N="SizeMB";E={[math]::round($_.SizeMB, 1)}})

Blog: http://www.virtual-matt.net

View solution in original post

Reply
0 Kudos
8 Replies
mattandes
Enthusiast
Enthusiast
Jump to solution

Try replacing this line:


$VMsWithSnaps = @(Get-VM | Get-Snapshot | Select vm,name,sizemb)


With this:


$VMsWithSnaps = @(Get-VM | Get-Snapshot | Select vm,name,@{N="SizeMB";E={[math]::round($_.SizeMB, 1)}})

Blog: http://www.virtual-matt.net
Reply
0 Kudos
ChrisJ615
Enthusiast
Enthusiast
Jump to solution

Here you go:

$VMsWithSnaps = @(Get-VM | Get-Snapshot | Select vm,name,{[int]$_.sizemb})

Give this a read if you get a moment.  It explains how [int] works Smiley Happy Windows PowerShell Tip: Formatting Numbers

LucD
Leadership
Leadership
Jump to solution

Casting to an [int] doesn't do rounding afaik


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ChrisJ615
Enthusiast
Enthusiast
Jump to solution

Sure it does:

PS C:\Users\chris> $test = 34.457454758739874837483

PS C:\Users\chris> [int]$test

34

PS C:\Users\chris>

Smiley Happy

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

To an [int] yes, but not to a decimal, that's why you use [math]::round


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ChrisJ615
Enthusiast
Enthusiast
Jump to solution

Correct - [int] uses the .Net method MidpointRounding Enumeration (System).  But whole numbers are a good thing, right. Smiley Wink

Either way the requirement was:

I would like it to round of to the nearest MB or 1 decimal place (either 34MB or 34.5MB)

I guess we both win. :smileycool:

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

There are no winners in this community, only users that get their questions answered 😉


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Bigi201110141
Enthusiast
Enthusiast
Jump to solution

Big Thanks. Both solutions worked.

Reply
0 Kudos