[Math]::Round

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
obrienc
Posts: 59
Last visit: Wed Apr 20, 2022 5:43 am

[Math]::Round

Post by obrienc »

I am fetching information about VMs, memory in particular. Some of the VMs have less than a gb and so I do a
  1. [Math]::Round(($vm.MemoryAssigned/ 1MB))
to give a nice readable number.

I also have VMs with much more memory so I do a
  1. [Math]::Round(($vm.MemoryAssigned/ 1GB))
These VMs can live on the same host and so the desire is to have same script pass each host. I am coming to the conclusion I have to write logic that will determine the length of the returned value of memory and based on that choose which operator to use.

My question is, is using a simple IF statement based on returned value the best way to solve this?
Thanks
User avatar
obrienc
Posts: 59
Last visit: Wed Apr 20, 2022 5:43 am

Re: [Math]::Round

Post by obrienc »

It would look something like this
  1. $v = $vm.MemoryAssigned|Measure -Character|Select -ExpandProperty Characters
  1. if ($v -le 9){
  2.             $Sheet2.Cells.Item($intRow, 4) = [Math]::Round(($vm.MemoryAssigned/ 1MB))
  3.                 }
  1. if ($v -ge 10){
  2.             $Sheet2.Cells.Item($intRow, 4) = [Math]::Round(($vm.MemoryAssigned/ 1GB))
  3. }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: [Math]::Round

Post by jvierra »

if($number -ge 1Gb){ # format as GB }else{ # format as Mb }

Just use the number and forget about strings.
User avatar
obrienc
Posts: 59
Last visit: Wed Apr 20, 2022 5:43 am

Re: [Math]::Round

Post by obrienc »

thanks
This topic is 6 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked