Powershell: Powershell should make it easier to use decimal values

Created on 9 Mar 2017  路  11Comments  路  Source: PowerShell/PowerShell

Steps to reproduce

Write 42.42 - 42 in the PowerShell prompt

Expected behavior

0.42 is returned

Actual behavior

0,420000000000002 is returned

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.0.0-alpha
PSEdition                      Core
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   3.0.0.0
GitCommitId                    v6.0.0-alpha.9
CLRVersion
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

A possible mitigation could be as could be as it is done in C# Interactive to do "42.42m - 42m" to force the evalutation as Decimal.

Issue-Enhancement Resolution-Answered WG-Language

Most helpful comment

Some comments:

  • PowerShell uses d for decimal because m was for 'mega'. m eventually turned into mb at a partners' request but that was later on.
  • FWIW the 'd' suffix e.g. 123d is what Visual Basic uses. Don't assume all PowerShell users are C# programmers.
  • 'd' for decimal is certainly more mnemonic.
  • Floating point operations in PowerShell are double by default so there's no suffix needed. You have to use the suffix f to force single precision floats. Don't assume all PowerShell users are C/C++ programmers.

All 11 comments

Sorry, misread your request.

Workaround:

PS /home/steve/repos/PowerShell> [decimal]42.42 -42                                            
0.42

The concern I have is this would be a breaking change

On the other hand think when you use it to do calculation on the shell? The user would expect the behavior of the calculator application and it gets an wrong result...

This obviously should not change the behavior in a script where 42.42 without indication of type should be always considered a double as is in C#.

@fanoI I agree that for interactive shell, I would prefer it to be a decimal for quick calculations. However, we try to keep the shell and scripting experience the same so there aren't any surprises switching from one to the other. Maybe for interactive we could do something with formatting so it looks like a decimal even though it's still a double.

I wouldn't change the default formatting because it is difficult to infer the expected precision people want to see, but it's relatively easy for folks to change that themselves.

We could add an m suffix though, I think that's a good idea.

Isn't d already defined for decimal literals?

@rkeithhill you're right

PS /home/steve> 42.42d - 42                                                                    
0.42

I implemented it and I still can't remember that, d will forever mean double to me.

We could still add m to align with C# - I'm not sure if that's a good idea or not though.

LOL. I remembered that this feature was added but my first guess was m as well. When that didn't work, I figured it might be d (for decimal).

Some comments:

  • PowerShell uses d for decimal because m was for 'mega'. m eventually turned into mb at a partners' request but that was later on.
  • FWIW the 'd' suffix e.g. 123d is what Visual Basic uses. Don't assume all PowerShell users are C# programmers.
  • 'd' for decimal is certainly more mnemonic.
  • Floating point operations in PowerShell are double by default so there's no suffix needed. You have to use the suffix f to force single precision floats. Don't assume all PowerShell users are C/C++ programmers.

It is possible to add a sort of configuration so that Decimal is used by default when calculations whit fractional numbers are done in the shell?

Was this page helpful?
0 / 5 - 0 ratings