Skip to content

Commit 3b847eb

Browse files
authored
Merge pull request #11575 from MicrosoftDocs/main
12/10/2024 PM Publish
2 parents 5a5aebd + f9bc7a4 commit 3b847eb

File tree

3 files changed

+165
-33
lines changed

3 files changed

+165
-33
lines changed

reference/5.1/Microsoft.PowerShell.Core/About/about_Type_Conversion.md

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Describes how and when PowerShell performs type conversions.
33
Locale: en-US
4-
ms.date: 12/05/2024
4+
ms.date: 12/10/2024
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_type_conversion?view=powershell-5.1&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_Type_Conversion
@@ -40,11 +40,11 @@ PS> $var.GetType().Name
4040
Int32
4141
```
4242

43-
Type casting ensures that only values of the specified type can be assigned to
44-
the variable. PowerShell performs an implicit conversion if you try to assign a
45-
value of a different type that can be converted to the constrained type. For
46-
more information, see the [Implicit type conversion][03] section of this
47-
article.
43+
Type constraining ensures that only values of the specified type can be
44+
assigned to the variable. PowerShell performs an implicit conversion if you try
45+
to assign a value of a different type that can be converted to the constrained
46+
type. For more information, see the [Implicit type conversion][03] section of
47+
this article.
4848

4949
### Numeric type conversion
5050

@@ -64,6 +64,21 @@ Byte
6464
The value `42.1` is a **Double**. When you cast it to a **Byte**, PowerShell
6565
truncates it to an integer `42`, which is small enough to fit into a **Byte**.
6666

67+
When converting real numbers to integer types, PowerShell uses rounding rather
68+
than truncation, specifically using the _rounding-to-nearest-even_ method.
69+
The following examples illustrate this behavior. Both values are rounded to the
70+
nearest even integer, `22`.
71+
72+
```powershell
73+
PS> [byte]21.5
74+
22
75+
PS> [byte]22.5
76+
22
77+
```
78+
79+
For more information, see the _Midpoint values and rounding conventions_
80+
section of the [Math.Round][21] method.
81+
6782
### Boolean type conversion
6883

6984
A value of _any_ type can be coerced to a **Boolean**.
@@ -85,7 +100,7 @@ A value of _any_ type can be coerced to a **Boolean**.
85100
```
86101

87102
- For other types, null values, empty strings, and empty arrays are converted
88-
to `$false`. Other values, including empty hashtables convert to `$true`.
103+
to `$false`.
89104

90105
```powershell
91106
PS> [boolean]''
@@ -94,7 +109,16 @@ A value of _any_ type can be coerced to a **Boolean**.
94109
False
95110
PS> [boolean]'Hello'
96111
True
97-
PS> [boolean]@(1,'Hello')
112+
```
113+
114+
Other values, including empty hashtables convert to `$true`. Single-element
115+
collections evaluate to the Boolean value of their one and only element.
116+
Collections with more than 1 element are always `$true`.
117+
118+
```powershell
119+
PS> [boolean]@(0)
120+
False
121+
PS> [boolean]@(0,0)
98122
True
99123
PS> [boolean]@{}
100124
True
@@ -285,8 +309,9 @@ This common code pattern is an instance of _pseudo method syntax_, which
285309
doesn't always work as intended. This syntax translates to:
286310

287311
```powershell
288-
[byte[]] $bytes = 1..16
289-
New-Object -TypeName System.Guid -ArgumentList $bytes # !! BROKEN
312+
PS> [byte[]] $bytes = 1..16
313+
PS> New-Object -TypeName System.Guid -ArgumentList $bytes
314+
New-Object: Cannot find an overload for "Guid" and the argument count: "16".
290315
```
291316

292317
Given that the type of **ArgumentList** is `[object[]]`, a single argument that
@@ -295,7 +320,8 @@ workaround is to wrap `$bytes` in an outer array so that PowerShell looks for a
295320
constructor with parameters that match the contents of the outer array.
296321

297322
```powershell
298-
PS> $guid = New-Object System.Guid(@(, $bytes))
323+
PS> [byte[]] $bytes = 1..16
324+
PS> $guid = New-Object -TypeName System.Guid -ArgumentList (, $bytes)
299325
PS> $guid
300326
301327
Guid
@@ -385,6 +411,23 @@ Int64
385411
The `L` suffix on the `1` literal forces the result to be an **Int64**. For
386412
more information, see [about_Numeric_Literals][07].
387413

414+
Using the `L` suffix is the preferred approach because it avoids loss of
415+
accuracy. If the type cast is applied after the calculation, you can lose
416+
accuracy due to the late type conversion. In the following example, the result
417+
is too large to fit into an **Int64** type so it is converted to a **Decimal**.
418+
419+
```powershell
420+
PS> '{0:N0}' -f ([Int64]::MaxValue + 1L)
421+
9,223,372,036,854,775,808
422+
```
423+
424+
Type casting the result after the calculation results in a loss in precision.
425+
426+
```powershell
427+
PS> '{0:N0}' -f [decimal]([Int64]::MaxValue + 1)
428+
9,223,372,036,854,780,000
429+
```
430+
388431
Usually, the left-hand side (LHS) operand of PowerShell operators determines
389432
the data type used in the operation. PowerShell converts (coerces) the
390433
right-hand side (RHS) operand to the required type.
@@ -490,3 +533,4 @@ For more information, see [about_Comparison_Operators][05].
490533
[18]: xref:System.Management.Automation.PSTypeConverter
491534
[19]: xref:System.Math.DivRem*
492535
[20]: xref:System.Math.Truncate*
536+
[21]: xref:System.Math.Round*#midpoint-values-and-rounding-conventions

reference/7.4/Microsoft.PowerShell.Core/About/about_Type_Conversion.md

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Describes how and when PowerShell performs type conversions.
33
Locale: en-US
4-
ms.date: 12/05/2024
4+
ms.date: 12/10/2024
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_type_conversion?view=powershell-7.4&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_Type_Conversion
@@ -40,11 +40,11 @@ PS> $var.GetType().Name
4040
Int32
4141
```
4242

43-
Type casting ensures that only values of the specified type can be assigned to
44-
the variable. PowerShell performs an implicit conversion if you try to assign a
45-
value of a different type that can be converted to the constrained type. For
46-
more information, see the [Implicit type conversion][03] section of this
47-
article.
43+
Type constraining ensures that only values of the specified type can be
44+
assigned to the variable. PowerShell performs an implicit conversion if you try
45+
to assign a value of a different type that can be converted to the constrained
46+
type. For more information, see the [Implicit type conversion][03] section of
47+
this article.
4848

4949
### Numeric type conversion
5050

@@ -64,6 +64,21 @@ Byte
6464
The value `42.1` is a **Double**. When you cast it to a **Byte**, PowerShell
6565
truncates it to an integer `42`, which is small enough to fit into a **Byte**.
6666

67+
When converting real numbers to integer types, PowerShell uses rounding rather
68+
than truncation, specifically using the _rounding-to-nearest-even_ method.
69+
The following examples illustrate this behavior. Both values are rounded to the
70+
nearest even integer, `22`.
71+
72+
```powershell
73+
PS> [byte]21.5
74+
22
75+
PS> [byte]22.5
76+
22
77+
```
78+
79+
For more information, see the _Midpoint values and rounding conventions_
80+
section of the [Math.Round][21] method.
81+
6782
### Boolean type conversion
6883

6984
A value of _any_ type can be coerced to a **Boolean**.
@@ -85,7 +100,7 @@ A value of _any_ type can be coerced to a **Boolean**.
85100
```
86101

87102
- For other types, null values, empty strings, and empty arrays are converted
88-
to `$false`. Other values, including empty hashtables convert to `$true`.
103+
to `$false`.
89104

90105
```powershell
91106
PS> [boolean]''
@@ -94,7 +109,16 @@ A value of _any_ type can be coerced to a **Boolean**.
94109
False
95110
PS> [boolean]'Hello'
96111
True
97-
PS> [boolean]@(1,'Hello')
112+
```
113+
114+
Other values, including empty hashtables convert to `$true`. Single-element
115+
collections evaluate to the Boolean value of their one and only element.
116+
Collections with more than 1 element are always `$true`.
117+
118+
```powershell
119+
PS> [boolean]@(0)
120+
False
121+
PS> [boolean]@(0,0)
98122
True
99123
PS> [boolean]@{}
100124
True
@@ -285,8 +309,9 @@ This common code pattern is an instance of _pseudo method syntax_, which
285309
doesn't always work as intended. This syntax translates to:
286310

287311
```powershell
288-
[byte[]] $bytes = 1..16
289-
New-Object -TypeName System.Guid -ArgumentList $bytes # !! BROKEN
312+
PS> [byte[]] $bytes = 1..16
313+
PS> New-Object -TypeName System.Guid -ArgumentList $bytes
314+
New-Object: Cannot find an overload for "Guid" and the argument count: "16".
290315
```
291316

292317
Given that the type of **ArgumentList** is `[object[]]`, a single argument that
@@ -295,7 +320,8 @@ workaround is to wrap `$bytes` in an outer array so that PowerShell looks for a
295320
constructor with parameters that match the contents of the outer array.
296321

297322
```powershell
298-
PS> $guid = New-Object System.Guid(@(, $bytes))
323+
PS> [byte[]] $bytes = 1..16
324+
PS> $guid = New-Object -TypeName System.Guid -ArgumentList (, $bytes)
299325
PS> $guid
300326
301327
Guid
@@ -385,6 +411,23 @@ Int64
385411
The `L` suffix on the `1` literal forces the result to be an **Int64**. For
386412
more information, see [about_Numeric_Literals][07].
387413

414+
Using the `L` suffix is the preferred approach because it avoids loss of
415+
accuracy. If the type cast is applied after the calculation, you can lose
416+
accuracy due to the late type conversion. In the following example, the result
417+
is too large to fit into an **Int64** type so it is converted to a **Decimal**.
418+
419+
```powershell
420+
PS> '{0:N0}' -f ([Int64]::MaxValue + 1L)
421+
9,223,372,036,854,775,808
422+
```
423+
424+
Type casting the result after the calculation results in a loss in precision.
425+
426+
```powershell
427+
PS> '{0:N0}' -f [decimal]([Int64]::MaxValue + 1)
428+
9,223,372,036,854,780,000
429+
```
430+
388431
Usually, the left-hand side (LHS) operand of PowerShell operators determines
389432
the data type used in the operation. PowerShell converts (coerces) the
390433
right-hand side (RHS) operand to the required type.
@@ -490,3 +533,4 @@ For more information, see [about_Comparison_Operators][05].
490533
[18]: xref:System.Management.Automation.PSTypeConverter
491534
[19]: xref:System.Math.DivRem*
492535
[20]: xref:System.Math.Truncate*
536+
[21]: xref:System.Math.Round*#midpoint-values-and-rounding-conventions

reference/7.5/Microsoft.PowerShell.Core/About/about_Type_Conversion.md

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
description: Describes how and when PowerShell performs type conversions.
33
Locale: en-US
4-
ms.date: 12/05/2024
4+
ms.date: 12/10/2024
55
online version: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_type_conversion?view=powershell-7.5&WT.mc_id=ps-gethelp
66
schema: 2.0.0
77
title: about_Type_Conversion
@@ -40,11 +40,11 @@ PS> $var.GetType().Name
4040
Int32
4141
```
4242

43-
Type casting ensures that only values of the specified type can be assigned to
44-
the variable. PowerShell performs an implicit conversion if you try to assign a
45-
value of a different type that can be converted to the constrained type. For
46-
more information, see the [Implicit type conversion][03] section of this
47-
article.
43+
Type constraining ensures that only values of the specified type can be
44+
assigned to the variable. PowerShell performs an implicit conversion if you try
45+
to assign a value of a different type that can be converted to the constrained
46+
type. For more information, see the [Implicit type conversion][03] section of
47+
this article.
4848

4949
### Numeric type conversion
5050

@@ -64,6 +64,21 @@ Byte
6464
The value `42.1` is a **Double**. When you cast it to a **Byte**, PowerShell
6565
truncates it to an integer `42`, which is small enough to fit into a **Byte**.
6666

67+
When converting real numbers to integer types, PowerShell uses rounding rather
68+
than truncation, specifically using the _rounding-to-nearest-even_ method.
69+
The following examples illustrate this behavior. Both values are rounded to the
70+
nearest even integer, `22`.
71+
72+
```powershell
73+
PS> [byte]21.5
74+
22
75+
PS> [byte]22.5
76+
22
77+
```
78+
79+
For more information, see the _Midpoint values and rounding conventions_
80+
section of the [Math.Round][21] method.
81+
6782
### Boolean type conversion
6883

6984
A value of _any_ type can be coerced to a **Boolean**.
@@ -85,7 +100,7 @@ A value of _any_ type can be coerced to a **Boolean**.
85100
```
86101

87102
- For other types, null values, empty strings, and empty arrays are converted
88-
to `$false`. Other values, including empty hashtables convert to `$true`.
103+
to `$false`.
89104

90105
```powershell
91106
PS> [boolean]''
@@ -94,7 +109,16 @@ A value of _any_ type can be coerced to a **Boolean**.
94109
False
95110
PS> [boolean]'Hello'
96111
True
97-
PS> [boolean]@(1,'Hello')
112+
```
113+
114+
Other values, including empty hashtables convert to `$true`. Single-element
115+
collections evaluate to the Boolean value of their one and only element.
116+
Collections with more than 1 element are always `$true`.
117+
118+
```powershell
119+
PS> [boolean]@(0)
120+
False
121+
PS> [boolean]@(0,0)
98122
True
99123
PS> [boolean]@{}
100124
True
@@ -285,8 +309,9 @@ This common code pattern is an instance of _pseudo method syntax_, which
285309
doesn't always work as intended. This syntax translates to:
286310

287311
```powershell
288-
[byte[]] $bytes = 1..16
289-
New-Object -TypeName System.Guid -ArgumentList $bytes # !! BROKEN
312+
PS> [byte[]] $bytes = 1..16
313+
PS> New-Object -TypeName System.Guid -ArgumentList $bytes
314+
New-Object: Cannot find an overload for "Guid" and the argument count: "16".
290315
```
291316

292317
Given that the type of **ArgumentList** is `[object[]]`, a single argument that
@@ -295,7 +320,8 @@ workaround is to wrap `$bytes` in an outer array so that PowerShell looks for a
295320
constructor with parameters that match the contents of the outer array.
296321

297322
```powershell
298-
PS> $guid = New-Object System.Guid(@(, $bytes))
323+
PS> [byte[]] $bytes = 1..16
324+
PS> $guid = New-Object -TypeName System.Guid -ArgumentList (, $bytes)
299325
PS> $guid
300326
301327
Guid
@@ -385,6 +411,23 @@ Int64
385411
The `L` suffix on the `1` literal forces the result to be an **Int64**. For
386412
more information, see [about_Numeric_Literals][07].
387413

414+
Using the `L` suffix is the preferred approach because it avoids loss of
415+
accuracy. If the type cast is applied after the calculation, you can lose
416+
accuracy due to the late type conversion. In the following example, the result
417+
is too large to fit into an **Int64** type so it is converted to a **Decimal**.
418+
419+
```powershell
420+
PS> '{0:N0}' -f ([Int64]::MaxValue + 1L)
421+
9,223,372,036,854,775,808
422+
```
423+
424+
Type casting the result after the calculation results in a loss in precision.
425+
426+
```powershell
427+
PS> '{0:N0}' -f [decimal]([Int64]::MaxValue + 1)
428+
9,223,372,036,854,780,000
429+
```
430+
388431
Usually, the left-hand side (LHS) operand of PowerShell operators determines
389432
the data type used in the operation. PowerShell converts (coerces) the
390433
right-hand side (RHS) operand to the required type.
@@ -490,3 +533,4 @@ For more information, see [about_Comparison_Operators][05].
490533
[18]: xref:System.Management.Automation.PSTypeConverter
491534
[19]: xref:System.Math.DivRem*
492535
[20]: xref:System.Math.Truncate*
536+
[21]: xref:System.Math.Round*#midpoint-values-and-rounding-conventions

0 commit comments

Comments
 (0)