Skip to content

Commit 582c6f1

Browse files
authored
Merge pull request #11596 from MicrosoftDocs/main
12/16/2024 PM Publish
2 parents 8b087b9 + 413e619 commit 582c6f1

File tree

7 files changed

+116
-91
lines changed

7 files changed

+116
-91
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ Describes how to create and use a reference type variable.
1717
You can pass variables to functions _by reference_ or _by value_. When you pass
1818
a variable _by value_, you are passing a copy of the data. When you pass a
1919
variable _by reference_, you are passing a reference to the original value.
20-
This allows the function to change the value of the variable that is passed to
21-
it.Reference types are created using `[ref]`, which is the type accelerator for
22-
the `[System.Management.Automation.PSReference]` type.
20+
This allows the function to change the value of the variable that's passed to
21+
it. Reference types are created using `[ref]`, which is the type accelerator
22+
for the `[System.Management.Automation.PSReference]` type.
2323

2424
The primary purpose of `[ref]` is to enable passing PowerShell variables by
2525
reference to .NET method parameters marked as `ref`, `out`, or `in`. You can
@@ -83,14 +83,14 @@ When using references, you must use the `Value` property of the `[ref]` type to
8383
access your data.
8484

8585
```powershell
86-
Function Test([ref]$data)
87-
{
86+
function Test {
87+
param([ref]$data)
8888
$data.Value = 3
8989
}
9090
```
9191

92-
To pass a variable to a parameter that expects a reference, you must type
93-
cast your variable as a reference.
92+
To pass a variable to a parameter that expects a reference, you must type cast
93+
your variable as a reference.
9494

9595
> [!IMPORTANT]
9696
> The brackets and parenthesis are BOTH required.
@@ -183,9 +183,9 @@ Get-ChildItem -File $setPath |
183183

184184
## Difference between `[ref]` and `[System.Management.Automation.PSReference]`
185185

186-
A reference type variable is created using
187-
188-
Even though `[ref]` is a type accelerator for
186+
A reference type variable is created using the `[ref]` type accelerator or by
187+
specifying the `[System.Management.Automation.PSReference]` type directly. Even
188+
though `[ref]` is a type accelerator for
189189
`[System.Management.Automation.PSReference]`, they behave differently.
190190

191191
- When you use `[ref]` to cast a variable, PowerShell creates a reference object

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

Lines changed: 27 additions & 19 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/10/2024
4+
ms.date: 12/16/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
@@ -77,7 +77,7 @@ PS> [byte]22.5
7777
```
7878

7979
For more information, see the _Midpoint values and rounding conventions_
80-
section of the [Math.Round][21] method.
80+
section of the [Math.Round][20] method.
8181

8282
### Boolean type conversion
8383

@@ -384,7 +384,7 @@ Double
384384
Even though both operands are integers, the result is converted to a **Double**
385385
to support the fractional result. To get true integer division, use the
386386
`[int]::Truncate()` or `[Math]::DivRem()` static methods. For more information,
387-
see [Truncate()][20] and [DivRem()][19].
387+
see [Truncate()][21] and [DivRem()][19].
388388

389389
In integer arithmetic, when the result overflows the size of the operands,
390390
PowerShell defaults to using **Double** for the results, even when the result
@@ -404,30 +404,37 @@ operands.
404404
```powershell
405405
PS> ([Int64]([int]::MaxValue + 1)).GetType().Name
406406
Int64
407-
PS> ([int]::MaxValue + 1L).GetType().Name
408-
Int64
409407
```
410408

411-
The `L` suffix on the `1` literal forces the result to be an **Int64**. For
412-
more information, see [about_Numeric_Literals][07].
413-
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**.
409+
However, use care when casting results to a specific type. For example, type
410+
casting the result to the `[decimal]` type can lead to loss of precision.
411+
Adding `1` to the maximum **Int64** value results in a **Double** type. When
412+
you cast a **Double** to a **Decimal** type, the result is
413+
`9223372036854780000`, which isn't accurate.
418414

419415
```powershell
420-
PS> '{0:N0}' -f ([Int64]::MaxValue + 1L)
421-
9,223,372,036,854,775,808
416+
PS> ([int64]::MaxValue + 1)GetType().Name
417+
Double
418+
PS> [decimal]([int64]::MaxValue + 1)
419+
9223372036854780000
422420
```
423421

424-
Type casting the result after the calculation results in a loss in precision.
422+
The conversion is limited to 15 digits of precision. For more information, see
423+
the _Remarks_ section of the [Decimal(Double)][01] constructor documentation.
424+
425+
To avoid a loss of precision, use the `D` suffix on the `1` literal. By adding
426+
the `D` suffix, PowerShell converts the `[int64]::MaxValue` to a **Decimal**
427+
before adding `1D`.
425428

426429
```powershell
427-
PS> '{0:N0}' -f [decimal]([Int64]::MaxValue + 1)
428-
9,223,372,036,854,780,000
430+
PS> ([int64]::MaxValue + 1D).GetType().Name
431+
Decimal
432+
PS> ([int64]::MaxValue + 1D)
433+
9223372036854775808
429434
```
430435

436+
For more information about numeric suffixes, see [about_Numeric_Literals][07].
437+
431438
Usually, the left-hand side (LHS) operand of PowerShell operators determines
432439
the data type used in the operation. PowerShell converts (coerces) the
433440
right-hand side (RHS) operand to the required type.
@@ -514,6 +521,7 @@ Both examples return true because `'true' -eq $true` yields `$true`.
514521
For more information, see [about_Comparison_Operators][05].
515522

516523
<!-- link references -->
524+
[01]: /dotnet/api/system.decimal.-ctor#system-decimal-ctor(system-double)
517525
[02]: /dotnet/csharp/programming-guide/statements-expressions-operators/overloadable-operators
518526
[03]: #implicit-type-conversion
519527
[04]: about_Booleans.md
@@ -532,5 +540,5 @@ For more information, see [about_Comparison_Operators][05].
532540
[17]: xref:System.Management.Automation.ArgumentTransformationAttribute
533541
[18]: xref:System.Management.Automation.PSTypeConverter
534542
[19]: xref:System.Math.DivRem*
535-
[20]: xref:System.Math.Truncate*
536-
[21]: xref:System.Math.Round*#midpoint-values-and-rounding-conventions
543+
[20]: xref:System.Math.Round*#midpoint-values-and-rounding-conventions
544+
[21]: xref:System.Math.Truncate*

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ Describes how to create and use a reference type variable.
1717
You can pass variables to functions _by reference_ or _by value_. When you pass
1818
a variable _by value_, you are passing a copy of the data. When you pass a
1919
variable _by reference_, you are passing a reference to the original value.
20-
This allows the function to change the value of the variable that is passed to
21-
it.Reference types are created using `[ref]`, which is the type accelerator for
22-
the `[System.Management.Automation.PSReference]` type.
20+
This allows the function to change the value of the variable that's passed to
21+
it. Reference types are created using `[ref]`, which is the type accelerator
22+
for the `[System.Management.Automation.PSReference]` type.
2323

2424
The primary purpose of `[ref]` is to enable passing PowerShell variables by
2525
reference to .NET method parameters marked as `ref`, `out`, or `in`. You can
@@ -83,14 +83,14 @@ When using references, you must use the `Value` property of the `[ref]` type to
8383
access your data.
8484

8585
```powershell
86-
Function Test([ref]$data)
87-
{
86+
function Test {
87+
param([ref]$data)
8888
$data.Value = 3
8989
}
9090
```
9191

92-
To pass a variable to a parameter that expects a reference, you must type
93-
cast your variable as a reference.
92+
To pass a variable to a parameter that expects a reference, you must type cast
93+
your variable as a reference.
9494

9595
> [!IMPORTANT]
9696
> The brackets and parenthesis are BOTH required.
@@ -183,9 +183,9 @@ Get-ChildItem -File $setPath |
183183

184184
## Difference between `[ref]` and `[System.Management.Automation.PSReference]`
185185

186-
A reference type variable is created using
187-
188-
Even though `[ref]` is a type accelerator for
186+
A reference type variable is created using the `[ref]` type accelerator or by
187+
specifying the `[System.Management.Automation.PSReference]` type directly. Even
188+
though `[ref]` is a type accelerator for
189189
`[System.Management.Automation.PSReference]`, they behave differently.
190190

191191
- When you use `[ref]` to cast a variable, PowerShell creates a reference object

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

Lines changed: 27 additions & 19 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/10/2024
4+
ms.date: 12/16/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
@@ -77,7 +77,7 @@ PS> [byte]22.5
7777
```
7878

7979
For more information, see the _Midpoint values and rounding conventions_
80-
section of the [Math.Round][21] method.
80+
section of the [Math.Round][20] method.
8181

8282
### Boolean type conversion
8383

@@ -384,7 +384,7 @@ Double
384384
Even though both operands are integers, the result is converted to a **Double**
385385
to support the fractional result. To get true integer division, use the
386386
`[int]::Truncate()` or `[Math]::DivRem()` static methods. For more information,
387-
see [Truncate()][20] and [DivRem()][19].
387+
see [Truncate()][21] and [DivRem()][19].
388388

389389
In integer arithmetic, when the result overflows the size of the operands,
390390
PowerShell defaults to using **Double** for the results, even when the result
@@ -404,30 +404,37 @@ operands.
404404
```powershell
405405
PS> ([Int64]([int]::MaxValue + 1)).GetType().Name
406406
Int64
407-
PS> ([int]::MaxValue + 1L).GetType().Name
408-
Int64
409407
```
410408

411-
The `L` suffix on the `1` literal forces the result to be an **Int64**. For
412-
more information, see [about_Numeric_Literals][07].
413-
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**.
409+
However, use care when casting results to a specific type. For example, type
410+
casting the result to the `[decimal]` type can lead to loss of precision.
411+
Adding `1` to the maximum **Int64** value results in a **Double** type. When
412+
you cast a **Double** to a **Decimal** type, the result is
413+
`9223372036854780000`, which isn't accurate.
418414

419415
```powershell
420-
PS> '{0:N0}' -f ([Int64]::MaxValue + 1L)
421-
9,223,372,036,854,775,808
416+
PS> ([int64]::MaxValue + 1)GetType().Name
417+
Double
418+
PS> [decimal]([int64]::MaxValue + 1)
419+
9223372036854780000
422420
```
423421

424-
Type casting the result after the calculation results in a loss in precision.
422+
The conversion is limited to 15 digits of precision. For more information, see
423+
the _Remarks_ section of the [Decimal(Double)][01] constructor documentation.
424+
425+
To avoid a loss of precision, use the `D` suffix on the `1` literal. By adding
426+
the `D` suffix, PowerShell converts the `[int64]::MaxValue` to a **Decimal**
427+
before adding `1D`.
425428

426429
```powershell
427-
PS> '{0:N0}' -f [decimal]([Int64]::MaxValue + 1)
428-
9,223,372,036,854,780,000
430+
PS> ([int64]::MaxValue + 1D).GetType().Name
431+
Decimal
432+
PS> ([int64]::MaxValue + 1D)
433+
9223372036854775808
429434
```
430435

436+
For more information about numeric suffixes, see [about_Numeric_Literals][07].
437+
431438
Usually, the left-hand side (LHS) operand of PowerShell operators determines
432439
the data type used in the operation. PowerShell converts (coerces) the
433440
right-hand side (RHS) operand to the required type.
@@ -514,6 +521,7 @@ Both examples return true because `'true' -eq $true` yields `$true`.
514521
For more information, see [about_Comparison_Operators][05].
515522

516523
<!-- link references -->
524+
[01]: /dotnet/api/system.decimal.-ctor#system-decimal-ctor(system-double)
517525
[02]: /dotnet/csharp/programming-guide/statements-expressions-operators/overloadable-operators
518526
[03]: #implicit-type-conversion
519527
[04]: about_Booleans.md
@@ -532,5 +540,5 @@ For more information, see [about_Comparison_Operators][05].
532540
[17]: xref:System.Management.Automation.ArgumentTransformationAttribute
533541
[18]: xref:System.Management.Automation.PSTypeConverter
534542
[19]: xref:System.Math.DivRem*
535-
[20]: xref:System.Math.Truncate*
536-
[21]: xref:System.Math.Round*#midpoint-values-and-rounding-conventions
543+
[20]: xref:System.Math.Round*#midpoint-values-and-rounding-conventions
544+
[21]: xref:System.Math.Truncate*

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ Describes how to create and use a reference type variable.
1717
You can pass variables to functions _by reference_ or _by value_. When you pass
1818
a variable _by value_, you are passing a copy of the data. When you pass a
1919
variable _by reference_, you are passing a reference to the original value.
20-
This allows the function to change the value of the variable that is passed to
21-
it.Reference types are created using `[ref]`, which is the type accelerator for
22-
the `[System.Management.Automation.PSReference]` type.
20+
This allows the function to change the value of the variable that's passed to
21+
it. Reference types are created using `[ref]`, which is the type accelerator
22+
for the `[System.Management.Automation.PSReference]` type.
2323

2424
The primary purpose of `[ref]` is to enable passing PowerShell variables by
2525
reference to .NET method parameters marked as `ref`, `out`, or `in`. You can
@@ -83,14 +83,14 @@ When using references, you must use the `Value` property of the `[ref]` type to
8383
access your data.
8484

8585
```powershell
86-
Function Test([ref]$data)
87-
{
86+
function Test {
87+
param([ref]$data)
8888
$data.Value = 3
8989
}
9090
```
9191

92-
To pass a variable to a parameter that expects a reference, you must type
93-
cast your variable as a reference.
92+
To pass a variable to a parameter that expects a reference, you must type cast
93+
your variable as a reference.
9494

9595
> [!IMPORTANT]
9696
> The brackets and parenthesis are BOTH required.
@@ -183,9 +183,9 @@ Get-ChildItem -File $setPath |
183183

184184
## Difference between `[ref]` and `[System.Management.Automation.PSReference]`
185185

186-
A reference type variable is created using
187-
188-
Even though `[ref]` is a type accelerator for
186+
A reference type variable is created using the `[ref]` type accelerator or by
187+
specifying the `[System.Management.Automation.PSReference]` type directly. Even
188+
though `[ref]` is a type accelerator for
189189
`[System.Management.Automation.PSReference]`, they behave differently.
190190

191191
- When you use `[ref]` to cast a variable, PowerShell creates a reference object

0 commit comments

Comments
 (0)