Metadata
Metadata copied to clipboard
Fix - Type `System.Decimal` loses precision during serialization and deserialization when the value contains floating-point digits
Summary
This pull request addresses the issue where the System.Decimal type loses precision during serialization and deserialization when the value contains floating-point digits.
Problem
When serializing and deserializing System.Decimal values, especially those with floating-point digits, there is a noticeable loss of precision. This can lead to incorrect values being processed in applications that rely on precise decimal calculations.
Reproduction
- Divide
System.Decimal::MaxValueby 10. - Serialize the Value using
ConvertTo-Metadata. - Deserialize the serialized value using
ConvertFrom-Metadata. - Compare the original and deserialized values.
Solution
- Append 'd' Suffix during serialization to the string representation of the
System.Decimalvalue. This ensures that during deserialization, the value is correctly interpreted as aSystem.Decimaland not mistakenly as aSystem.Double.
Example
[String]( [Decimal]::MaxValue ) #* 79228162514264337593543950335
[String]( [Decimal]::MaxValue | ConvertTo-Metadata ) #* 79228162514264337593543950335
[String]( ConvertFrom-Metadata -InputObject $([Decimal]::MaxValue | ConvertTo-Metadata) ) #* 79228162514264337593543950335
[String]( [Decimal]::MaxValue/10 ) #* 7922816251426433759354395033.5
[String]( [Decimal]::MaxValue/10 | ConvertTo-Metadata ) #* 7922816251426433759354395033.5
[String]( ConvertFrom-Metadata -InputObject $([Decimal]::MaxValue/10 | ConvertTo-Metadata) ) #! 7.92281625142643E+27 ($_ -Is [Double])
[String]( ConvertFrom-Metadata -InputObject "$([Decimal]::MaxValue/10 | ConvertTo-Metadata)d" ) #* 7922816251426433759354395033.5
Links
- Fixes: #9