Automation

 View Only
  • 1.  Decimal round of size

    Posted Jun 26, 2013 10:34 PM

    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++}



  • 2.  RE: Decimal round of size
    Best Answer

    Posted Jun 26, 2013 11:06 PM

    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)}})



  • 3.  RE: Decimal round of size

    Posted Jun 27, 2013 05:27 AM

    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 :smileyhappy: Windows PowerShell Tip: Formatting Numbers



  • 4.  RE: Decimal round of size

    Posted Jun 27, 2013 05:30 AM

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



  • 5.  RE: Decimal round of size

    Posted Jun 27, 2013 05:34 AM

    Sure it does:

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

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

    34

    PS C:\Users\chris>

    :smileyhappy:



  • 6.  RE: Decimal round of size

    Posted Jun 27, 2013 05:39 AM

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



  • 7.  RE: Decimal round of size

    Posted Jun 27, 2013 06:30 AM

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

    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:



  • 8.  RE: Decimal round of size

    Posted Jun 27, 2013 06:34 AM

    There are no winners in this community, only users that get their questions answered ;-)



  • 9.  RE: Decimal round of size

    Posted Jun 29, 2013 12:22 AM

    Big Thanks. Both solutions worked.