Skip to content

Commit 9c87174

Browse files
committed
Fix decimal conversion examples
1 parent c53d1b3 commit 9c87174

File tree

3 files changed

+81
-57
lines changed

3 files changed

+81
-57
lines changed

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_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_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.5&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*

0 commit comments

Comments
 (0)