From 62d6f918df7e39c045bb7c358d414d679cade0b5 Mon Sep 17 00:00:00 2001 From: Gabriel Adrian Samfira Date: Wed, 18 Dec 2024 12:17:17 +0200 Subject: [PATCH] Use decimal as first option when parsing floats Decimals offer much greater precission as opposed to double at the cost of performance. However, in most cases, precission is desirable. Signed-off-by: Gabriel Adrian Samfira --- Tests/powershell-yaml.Tests.ps1 | 9 +++++++++ powershell-yaml.psm1 | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Tests/powershell-yaml.Tests.ps1 b/Tests/powershell-yaml.Tests.ps1 index dcdac17..3dd682d 100644 --- a/Tests/powershell-yaml.Tests.ps1 +++ b/Tests/powershell-yaml.Tests.ps1 @@ -703,6 +703,7 @@ bools: bigInt: 99999999999999999999999999999999999 int32: 2147483647 int64: 9223372036854775807 +decimal: 3.10 '@ } @@ -711,11 +712,19 @@ int64: 9223372036854775807 $result.bigInt | Should -BeOfType System.Numerics.BigInteger } + It "Should round-trip decimals with trailing 0" { + $result = ConvertFrom-Yaml -Yaml $value + $result.decimal | Should -Be ([decimal]3.10) + + ConvertTo-Yaml $result["decimal"] | Should -Be "3.10$([Environment]::NewLine)" + } + It 'Should be of proper type and value' { $result = ConvertFrom-Yaml -Yaml $value $result.bigInt | Should -Be ([System.Numerics.BigInteger]::Parse("99999999999999999999999999999999999")) $result.int32 | Should -Be ([int32]2147483647) $result.int64 | Should -Be ([int64]9223372036854775807) + $result.decimal | Should -Be ([decimal]3.10) } } diff --git a/powershell-yaml.psm1 b/powershell-yaml.psm1 index 8ee2ec4..255a123 100644 --- a/powershell-yaml.psm1 +++ b/powershell-yaml.psm1 @@ -218,7 +218,7 @@ function Convert-ValueToProperType { } return $parsedValue } - $types = @([double], [decimal]) + $types = @([decimal], [double]) foreach($i in $types){ $parsedValue = New-Object -TypeName $i.FullName $result = $i::TryParse($Node, [Globalization.NumberStyles]::Float, [Globalization.CultureInfo]::InvariantCulture, [ref]$parsedValue)