Weekend Scripter: PowerShell Does Scientific Notation

Weekend Scripter: PowerShell Does Scientific Notation

  • Comments 3
  • Likes

Summary: Microsoft PFE and guest blogger, Clint Huffman, talks about scientific notation and Windows PowerShell.

Microsoft Scripting Guy, Ed Wilson, is here. Today we have another guest blogger from the past—Clint Huffman. The keyboard is yours Clint…

In Performance Monitor, if a number in a field is too large to display, scientific notation is used to represent the number. A value such as 30,064,771,072 (28 GB) is too large for a field to display, so it is represented as 3.0064e+010. This is not an error—it is scientific notation. It indicates that the decimal point must be moved to the right the number of times indicated after the plus (+) symbol. There are 9 digits behind a gigabyte, so the number 2.8637e+010 in the following image appears as a “ball park” figure of 28 GB. But after dividing each thousands group by 1024, the true answer is 26.7 GB.

Image of menu

Math is hard for me, and I avoid it when I can, so I use Windows PowerShell to help me with it. Windows PowerShell can convert values in gigabytes and return the number in bytes or vice-versa. For example, Windows PowerShell can convert 35GB into the full numeric value of 37580963840 in bytes.

Image of command output

It can convert scientific notation into a full numeric value. For example, in the previous image, I typed the value 3.7580e+010 from Perfmon into Windows PowerShell and it returned 37580000000. Knowing what this number is in gigabytes would be nice, so I divided it by 1GB (Windows PowerShell understands KB as kilobytes, MB as megabytes, and so on) to give me the number in gigabytes. In this case, it didn’t quite return an even 35 GB because the actual number was truncated by Perfmon, but it is close enough.

Image of command output

The editor in me has to add this…

Proper technical documentation is to use a space between the number and the unit, such as 10 GB. The reason I specified 35GB earlier is because in order for Windows PowerShell to interpret the number as 35 gigabytes, the space must be removed between number and the unit—35<space>GB will throw an error.

Enjoy!

~Clint

Cool. Thank you, Clint. I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Here is a real sn getter.

    PS > '{0:E}' -f 0

    0.000000E+000

    PS > '{0:E}' -f 1

    1.000000E+000

    PS > '{0:E}' -f 1Gb

    1.073742E+009

    PS > '{0:E}' -f 10000000000

    1.000000E+010

    See - it just counts from 1 and tells us how many zeros we are using.

    we can also do this nifty little thing:

    PS >(1).ToString('E')

    1.000000E+000

    PS > ((5*33)*(23*765)).ToString('E')

    2.903175E+006

    Scientific notations induce error into the math.  WHere does this error begin.  In other words what is the resolution of our engine?

    On my 64 bit Intel this is the resolution:

    PS  12345678901234567890123456789

    12345678901234567890123456789

    PS > 123456789012345678901234567890

    1.23456789012346E+29

  • Good to Know!!!

  • Helps in many scripts for Disk Space and formatting Sizes in MB, GB etc. Thanks !!!