Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use decimal as first option when parsing floats #170

Merged

Conversation

gabriel-samfira
Copy link
Member

@gabriel-samfira gabriel-samfira commented Dec 18, 2024

Decimals offer much greater precission as opposed to double at the cost of performance. However, in most cases, precission is desirable.

Thanks @amis92 !

Fixes: #152

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 <gsamfira@cloudbasesolutions.com>
@gabriel-samfira gabriel-samfira merged commit 4c0f8e9 into cloudbase:master Dec 18, 2024
7 checks passed
@gabriel-samfira gabriel-samfira deleted the prioritize-decimal branch December 18, 2024 13:37
@@ -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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd add a test for .Scale here.

The [decimal]3.10 is not really working in PowerShell as you might expect - this firstly creates a double literal, and then that is converted to decimal object. So in reality, the [decimal]3.10 would have Scale=1 and will print (via ToString e.g.) as 3.1. If you construct decimal via explicit Parsing though, it will correctly have Scale=2. See:

$scaletest = '3.9999999999999990'
[decimal]3.9999999999999990
# prints 4
[decimal]$scaletest
# prints 4
[decimal]::Parse($scaletest, [cultureinfo]::InvariantCulture)
# prints `3.9999999999999990`

@amis92
Copy link

amis92 commented Dec 18, 2024

Saw it too late, but it's a comment on test only, and doesn't really change much. Most important test of round-trip works as expected.

Great change, thank you!

@gabriel-samfira
Copy link
Member Author

adding a few more test cases, which will include this one as well. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrectly read the version number
2 participants