Understanding Numbers in PowerShell
Summary: Microsoft Scripting Guy, Ed Wilson, talks about numbers in Windows PowerShell.
Hey, Scripting Guy! One of the things that is frustrating to me with Windows PowerShell is the way that it handles numbers. For example, I want to know the percent of processor utilization for something, and it displays a number that is like fifty million numbers long. What is up with that? Why can’t Windows PowerShell just tell me something useful?
Microsoft Scripting Guy, Ed Wilson, is here. Today it is cold and raining outside. I decided to make a cup of red berry tea. It does not have any caffeine, and it seems to be just thing for a cold, rainy morning. I do not mix my own red berry tea—this is one of the teas that I buy already mixed. I still had a nice bag I brought back from Hamburg. Anyway, I am sitting here sipping a nice cup of red berry tea and I came across your email to firstname.lastname@example.org. It is perfect because today I am kicking off Numbers Week.
There are many cmdlets and functions built-in to Windows PowerShell that display a formatted number. But there are other interfaces that return the raw number. The reason for this is that I may want to display information to 1, 2, 3, or even 5 decimal places, depending on what I am monitoring. In many cases, I work with the entire raw number so I can get a better view of what exactly might be taking place. For this reason, it makes sense to understand a bit about the types of numbers in Windows PowerShell.
There are many types of numbers in Windows PowerShell. Most of the time, I do not need to know anything about the different types of numbers because Windows PowerShell performs the type conversion behind the scenes. However, a good understanding of the types of numbers can be useful, and it is certainly useful from a troubleshooting perspective. There are actually three numeric types that Windows PowerShell uses:
- Decimal types
- Integral types
- Floating-point types
Decimals are a 128-bit data type. Decimals can be positive or negative, and they are more precise than floating point types. Decimals are commonly used in financial or monetary calculations. A decimal is an instance of the System.Decimal .NET Framework type, and therefore, it has static properties I can use to determine the range. Here is the minimum and maximum range of the decimal numeric type:
PS C:> [decimal]::MinValue
PS C:> [decimal]::MaxValue
This range is commonly expressed as (-7.9 x 1028 to 7.9 x 1028) / (100 to 28).
Integral types come in the signed and the unsigned variety (except for the char, which is an 16-bit Unicode character). Integers range in size from 8-bit integers to 64-bit integers.
An sbyte is a signed 8-bit integer, and it ranges from -128 to 127. A byte is an unsigned 8-bit integer that ranges from 0 to 255. The following code illustrates this:
PS C:> [byte]::MinValue
PS C:> [byte]::MaxValue
PS C:> [sbyte]::MinValue
PS C:> [sbyte]::MaxValue
A short is a signed 16-bit integer, and a ushort is an unsigned 16-bit integer. To obtain a short, I use the System.Int16 .NET Framework class, and to obtain a ushort, I use System.uInt16. I can use the [int16] and the [uint16] type accelerators for this purpose. The following code illustrates their ranges:
PS C:> [int16]::MinValue
PS C:> [int16]::MaxValue
PS C:> [uint16]::MinValue
PS C:> [uint16]::MaxValue
Int is the default numeric data type in Windows PowerShell. It is a 32-bit signed integer. The .NET Framework class is System.Int32. Because it is the default numeric data type, I can use [int32] or [int]. There is also an unsigned 32-bit integer. It is the System.uint32 .NET Framework type. I can use [uint32] to create an unsigned 32-bit integer. The ranges of these numbers are shown here:
PS C:> [int32]::MinValue
PS C:> [int32]::MaxValue
PS C:> [int]::MaxValue
PS C:> [uint32]::MinValue
PS C:> [uint32]::MaxValue
A long is a signed 64-bit integer and a ulong is an unsigned 64-bit integer. The .NET Framework classes are System.Int64 and System.uInt64. I can therefore use [int64] or [uint64] to create the long or the ulong data types. The following code illustrates the ranges of the long and ulong:
PS C:> [int64]::MinValue
PS C:> [int64]::MaxValue
PS C:> [uint64]::MinValue
PS C:> [uint64]::MaxValue
There are two floating-point types that Windows PowerShell uses: the float and the double. The float uses seven digits of precision and the double uses 15–16 digits of precision. The float type is an instance of the System.Single .NET Framework value type, and the double is an instance of the System.Double type. I can therefore use [single] and [double] to constrain numbers to these types. The following code illustrates their minimum and maximum values:
PS C:> [single]::MinValue
PS C:> [single]::MaxValue
PS C:> [double]::MinValue
PS C:> [double]::MaxValue
The following table summarizes the numeric value types, their ranges, sizes, and .NET Framework types.
Size or Precision
.NET Framework type
(-7.9 x 1028 to 7.9 x 1028) / (100 to 28)
28 – 29 significant digits
-128 to 127
0 to 255
U+0000 to U+ffff
-32,768 to 32,767
0 to 65,535
-2,147,483,648 to 2,147,483,647
Signed 32 bit
0 to 4,294,967,295
9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
0 to 18,446,744,073,709,551,615
±1.5e−45 to ±3.4e38
±5.0e−324 to ±1.7e308
15 – 16 digits
That is all there is to using Windows PowerShell to format numbers, SP. Numbers Week will continue tomorrow when I will talk about more cool stuff.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy