Skip to content

Commit 3692cef

Browse files
shethaaditAdit Sheth
andauthored
Clarify Decimal to Single/Double Conversion and Precision Loss (#44144)
* Fixed bug 44001. * Fixed bug 39795. --------- Co-authored-by: Adit Sheth <[email protected]>
1 parent d6bf443 commit 3692cef

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

docs/visual-basic/language-reference/data-types/decimal-data-type.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,22 @@ The default value of `Decimal` is 0.
3838

3939
- **Performance.** The `Decimal` data type is the slowest of all the numeric types. You should weigh the importance of precision against performance before choosing a data type.
4040

41-
- **Widening.** The `Decimal` data type widens to `Single` or `Double`. This means you can convert `Decimal` to either of these types without encountering a <xref:System.OverflowException?displayProperty=nameWithType> error.
41+
- **Widening.** The `Decimal` data type can be converted to `Single` or `Double` without encountering a <xref:System.OverflowException?displayProperty=nameWithType> error. However, this conversion may result in **loss of precision**, as `Single` and `Double` prioritize accommodating larger values over preserving precision.
42+
43+
```vb
44+
Dim decimalValue As Decimal = 1234567890123456789012345D
45+
Dim doubleValue As Double = CDbl(decimalValue)
46+
47+
Console.WriteLine("Decimal value: " & decimalValue)
48+
Console.WriteLine("Double value: " & doubleValue)
49+
```
50+
51+
The output of the preceding example shows that the **Decimal value** retains full precision, while the **Double value** loses precision due to the limitations of the `Double` data type.
52+
53+
```console
54+
Decimal value: 1234567890123456789012345
55+
Double value: 1.23456789012346E+24
56+
```
4257

4358
- **Trailing Zeros.** Visual Basic does not store trailing zeros in a `Decimal` literal. However, a `Decimal` variable preserves any trailing zeros acquired computationally. The following example illustrates this.
4459

0 commit comments

Comments
 (0)