Manipulating Object

This topic contains 2 replies, has 3 voices, and was last updated by  Curtis Smith 1 year, 10 months ago.

  • Author
  • #32653

    $memory   = Get-WmiObject -ComputerName $computerName  win32_physicalmemory
     RAMtotal =  (($memory.capacity | Measure-Object -sum).sum)/1gb

    I am trying to format this with two number to the right of the decimal. "N0 {0.00} formatted" the values I have are objects not INT. How do I clean this up?

  • #32655

    Peter Jurgens

    To use formatting you can use the -f operator but bear in mind that this only returns strings so sorting may not work as you'd expect. Here's an example:

    "{0:N2}" -f [Math]::Pi

    This takes the values to the right of the -f operator, inserts it into the string "template" at position 0 (denoted by the first 0) and formats it as a number with 2 decimal places (N2)

  • #32657

    Curtis Smith

    Use the Math.Round method if you are trying to crop off lots of extra decimal values, but if you are just trying to always have 2 decimal places, even if it's just a whole number, Peter's got your answer.

    $memory   = Get-WmiObject win32_physicalmemory
    $RAMtotal =  [Math]::Round((($memory.capacity | Measure-Object -sum).sum)/1gb, 2)

You must be logged in to reply to this topic.