{"id":8161,"date":"2015-01-26T00:01:00","date_gmt":"2015-01-26T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/01\/26\/understanding-numbers-in-powershell\/"},"modified":"2019-02-18T10:35:47","modified_gmt":"2019-02-18T17:35:47","slug":"understanding-numbers-in-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/understanding-numbers-in-powershell\/","title":{"rendered":"Understanding Numbers in PowerShell"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about numbers in Windows PowerShell.<\/span>\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\">&nbsp;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&rsquo;t Windows PowerShell just tell me something useful?\n&mdash;SP\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\">&nbsp;Hello SP,\nMicrosoft 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&mdash;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 <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>. It is perfect because today I am kicking off Numbers Week.\nThere 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.<\/p>\n<h2>Numeric types<\/h2>\n<p>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:<\/p>\n<ul>\n<li>Decimal types<\/li>\n<li>Integral types<\/li>\n<li>Floating-point types<\/li>\n<\/ul>\n<h3>Decimal types<\/h3>\n<p>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:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; [decimal]::MinValue<\/p>\n<p style=\"margin-left:30px\">-79228162514264337593543950335<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; [decimal]::MaxValue<\/p>\n<p style=\"margin-left:30px\">79228162514264337593543950335\nThis range is commonly expressed as (-7.9 x 10<sup>28<\/sup> to 7.9 x 10<sup>28<\/sup>) \/ (10<sup>0&nbsp;to&nbsp;28<\/sup>).<\/p>\n<h3>Integral types<\/h3>\n<p>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.\nAn <b>sbyte<\/b> is a signed 8-bit integer, and it ranges from -128 to 127. A <b>byte<\/b> is an unsigned 8-bit integer that ranges from 0 to 255. The following code illustrates this:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; [byte]::MinValue<\/p>\n<p style=\"margin-left:30px\">0<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; [byte]::MaxValue<\/p>\n<p style=\"margin-left:30px\">255<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; [sbyte]::MinValue<\/p>\n<p style=\"margin-left:30px\">-128<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; [sbyte]::MaxValue<\/p>\n<p style=\"margin-left:30px\">127\nA <b>short<\/b> is a signed 16-bit integer, and a <b>ushort<\/b> 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:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; [int16]::MinValue<\/p>\n<p style=\"margin-left:30px\">-32768<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; [int16]::MaxValue<\/p>\n<p style=\"margin-left:30px\">32767<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; [uint16]::MinValue<\/p>\n<p style=\"margin-left:30px\">0<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; [uint16]::MaxValue<\/p>\n<p style=\"margin-left:30px\">65535\n<b>Int<\/b> 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:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; [int32]::MinValue<\/p>\n<p style=\"margin-left:30px\">-2147483648<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; [int32]::MaxValue<\/p>\n<p style=\"margin-left:30px\">2147483647<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; [int]::MaxValue<\/p>\n<p style=\"margin-left:30px\">2147483647<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; [uint32]::MinValue<\/p>\n<p style=\"margin-left:30px\">0<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; [uint32]::MaxValue<\/p>\n<p style=\"margin-left:30px\">4294967295\nA <b>long<\/b> is a signed 64-bit integer and a <b>ulong<\/b> 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:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; [int64]::MinValue<\/p>\n<p style=\"margin-left:30px\">-9223372036854775808<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; [int64]::MaxValue<\/p>\n<p style=\"margin-left:30px\">9223372036854775807<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; [uint64]::MinValue<\/p>\n<p style=\"margin-left:30px\">0<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; [uint64]::MaxValue<\/p>\n<p style=\"margin-left:30px\">18446744073709551615<\/p>\n<h3>Floating-point types<\/h3>\n<p>There are two floating-point types that Windows PowerShell uses: the <b>float<\/b> and the <b>double<\/b>. The float uses seven digits of precision and the double uses 15&ndash;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:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; [single]::MinValue<\/p>\n<p style=\"margin-left:30px\">-3.402823E+38<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; [single]::MaxValue<\/p>\n<p style=\"margin-left:30px\">3.402823E+38<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; [double]::MinValue<\/p>\n<p style=\"margin-left:30px\">-1.79769313486232E+308<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; [double]::MaxValue<\/p>\n<p style=\"margin-left:30px\">1.79769313486232E+308\nThe following table summarizes the numeric value types, their ranges, sizes, and .NET Framework types.<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td width=\"160\" valign=\"top\">  <b>Type<\/b><\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p><b>Range<\/b><\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p><b>Size or Precision<\/b><\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p><b>.NET Framework type<\/b><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"160\" valign=\"top\">\n<p>Decimal<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>(-7.9 x 10<sup>28<\/sup> to 7.9 x 10<sup>28<\/sup>) \/ (10<sup>0&nbsp;to&nbsp;28<\/sup>)<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>28 &ndash; 29 significant digits<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>System.Decimal<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"160\" valign=\"top\">\n<p>Sbyte<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>-128 to 127<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>Signed 8-bit<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>System.Sbyte<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"160\" valign=\"top\">\n<p>Byte<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>0 to 255<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>Unsigned 8-bit<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>System.Byte<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"160\" valign=\"top\">\n<p>Char<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>U+0000 to U+ffff<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>Unicode 16-bit<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>System.Char<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"160\" valign=\"top\">\n<p>Short<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>-32,768 to 32,767<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>Signed 16-bit<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>System.Int16<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"160\" valign=\"top\">\n<p>Ushort<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>0 to 65,535<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>Unsigned 16-bit<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>System.Uint16<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"160\" valign=\"top\">\n<p>Int<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>-2,147,483,648 to 2,147,483,647<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>Signed 32 bit<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>System.Int32<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"160\" valign=\"top\">\n<p>Uint<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>0 to 4,294,967,295<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>Unsigned 32-bit<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>System.Uint32<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"160\" valign=\"top\">\n<p>Long<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>9,223,372,036,854,775,808 to 9,223,372,036,854,775,807<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>Singed 64-bit<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>System.Int64<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"160\" valign=\"top\">\n<p>Ulong<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>0 to 18,446,744,073,709,551,615<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>Unsigned 64-bit<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>System.Uint64<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"160\" valign=\"top\">\n<p>Float<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>&plusmn;1.5e&minus;45 to &plusmn;3.4e38<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>7 digits<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>System.Single<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"160\" valign=\"top\">\n<p>Double<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>&plusmn;5.0e&minus;324 to &plusmn;1.7e308<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>15 &ndash; 16 digits<\/p>\n<\/td>\n<td width=\"160\" valign=\"top\">\n<p>System.Double<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-size:12px\">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.<\/span>\nI invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about numbers in Windows PowerShell. &nbsp;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 [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,398,3,45],"class_list":["post-8161","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-numbers","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about numbers in Windows PowerShell. &nbsp;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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/8161","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=8161"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/8161\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=8161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=8161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=8161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}