VMware Cloud Community
AutomationStat1
Contributor
Contributor

How to round storage output numbers

How and where would I add a rounding operator to round the inventory script I have showing how much storage is being used? I would like to only show one number after the decimal point. Anyone know?

Get-VM | Select-Object Name,MemoryGB,NumCpu,UsedSpaceGB,@{n="TotalHDSizeGB"; e={(Get-HardDisk -VM $_ |Measure-Object -Sum CapacityGB).Sum}},@{n="Network"; e={(Get-NetworkAdapter -VM $_ |Select -unique -expand NetworkName)}} | Sort-Object Network

Reply
0 Kudos
2 Replies
AutomationStat1
Contributor
Contributor

pastedImage_0.png

Above shows one output with used space and way too many numbers after the decimal

Reply
0 Kudos
mattboren
Expert
Expert

Hello, AutomationStation -

You can use the Round() method of the .NET class Math to round your numbers.  For example, to round to one decimal place:

PS C:\> [System.Math]::Round(123.42324, 1)

123.4

So, for your example, you could use it as such:

Get-VM | Select-Object Name, MemoryGB, NumCpu,

    @{n="UsedSpaceGB"; e={[System.Math]::Round($_.UsedSpaceGB, 1)}},

    @{n="TotalHDSizeGB"; e={(Get-HardDisk -VM $_ | Measure-Object -Sum CapacityGB).Sum}},

    @{n="Network"; e={(Get-NetworkAdapter -VM $_ | Select -Unique -Expand NetworkName)}} | Sort-Object Network

How does that do?

Reply
0 Kudos